Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将一个元素作为弹出窗口仅打开给特定的用户_Javascript_Jquery_Html_Dom_Math - Fatal编程技术网

Javascript 如何将一个元素作为弹出窗口仅打开给特定的用户

Javascript 如何将一个元素作为弹出窗口仅打开给特定的用户,javascript,jquery,html,dom,math,Javascript,Jquery,Html,Dom,Math,我有一个js的,这是集成到其他网站。当用户进入站点时,将从脚本调用一个函数,并弹出一个元素。 对于我来说,在弹出元素的js函数中,是否可以让它只对一定比例(比如说60%)的用户开放? 我曾考虑过使用Math.random()函数,但我不知道如何实现它 编辑: 考虑到这一点,可能仅仅通过javascript是无法实现的,它需要使用某种类型的用户跟踪(通过数据库等)。如果有人知道另一种方法,我很乐意听到。使用Math.Random()是一个很好的选择Math.Random()生成一个介于0和1之间的

我有一个js的,这是集成到其他网站。当用户进入站点时,将从脚本调用一个函数,并弹出一个元素。 对于我来说,在弹出元素的js函数中,是否可以让它只对一定比例(比如说60%)的用户开放? 我曾考虑过使用
Math.random()
函数,但我不知道如何实现它

编辑: 考虑到这一点,可能仅仅通过javascript是无法实现的,它需要使用某种类型的用户跟踪(通过数据库等)。如果有人知道另一种方法,我很乐意听到。

使用
Math.Random()
是一个很好的选择
Math.Random()
生成一个介于0和1之间的数字,因此如果您想要一个介于0和10之间的随机数,请将结果乘以10,然后取该数字的上限以删除小数点并将其转换为整数。对于0到100,将其乘以100。在弹出函数的开头,添加以下内容:

var rand=Math.Random()*100

接下来,将显示弹出窗口的代码包装在if语句中,该语句检查生成的随机数是否小于或等于60:

if(Math.ceil(rand) <= 60) {
    ...
}

if(Math.ceil(rand)以下是我在需要此功能时所做的一个示例

如果您更改选项的数量或机会百分比,那么下面的代码将更易于维护

var contentId, random = Math.random();

if (random < 0.5) {
  // option 1: chance 0.0–0.499...
  contentId = 0;
} else (random < 0.75) {
  // option 2: chance 0.50—0.7499...
  contentId = 1;
} else {
  // option 3: chance 0.75–0.99...
  contentId = 2;
}

loadContent(contentId);
var contentId,random=Math.random();
if(随机<0.5){
//选项1:机会0.0–0.499。。。
contentId=0;
}其他(随机<0.75){
//选项2:机会0.50-0.7499。。。
contentId=1;
}否则{
//选项3:机会0.75–0.99。。。
contentId=2;
}
loadContent(contentId);
根据评论反馈进行编辑


很高兴提供帮助。如果这是一个php页面,那么您不需要ajax。只需通过php/sql更新/返回页面顶部的计数,然后将该数字
echo
输入JS函数。您只需检查该数字是否可被
3
整除。如果是,则运行弹出函数。这样您还可以报告该数字弹出窗口会被激活的次数。

它很容易通过Javascript实现,而且非常简单

var pctg = 60; // Set percentage here
var rndm = Math.ceil(Math.Random() * 100);

if (rndm <= pctg) {
    // Execute popup statement(s)
}
请查看有关cookies使用的更多信息

接下来我们可以这样做:

// See if the user already received the popup
// The if statement will only evaluate to true if the popup is *not* explicitly disabled (that is, it is set to 1, or not set)
if (getCookie('bShowPopup') !== '0')
{
    var pctg = 60; // Set percentage here
    var rndm = Math.ceil(Math.Random() * 100);

    if (rndm <= pctg) {
        // Execute popup statement(s)
        //stmt123....

        // Set cookie bShowPopup to 0 (so the user won't receive popups ever again)
        // NOTE: The cookie will expire after 30 days.
        setCookie('bShowPopup', '0', 30);
    }
}

请同时参阅有关Math.random()的使用和返回有效期的信息。

Math.random()如何保证一定的百分比?如果
Math.random()
始终返回值小于60,则所有用户(100%)我可以看到弹出窗口,但我认为没有可能达到一定的百分比。问题是,如果我有100个用户进入页面,并且我得到100次随机<0.5,那么我只是向100%的用户显示弹出窗口…除非您将此数据存储在数据库中,以便您可以根据访问者数量选择显示,否则您将无法实现如果Javascript在最后一个“x”中已经显示了此页面,它将无法“知道”它是否已经显示了此页面用户数量。如果这是通过ajax和php完成的,那么您将保存统计数据,因此您可以进行简单准确的计算。我想这正是我想要的答案。我不确定单独使用javascript是否可行,现在我知道我必须使用我的数据库才能实现这一点。谢谢!谢谢,这一行为Oly给了我很多帮助,加上我不得不添加的一些服务器端代码。
// See if the user already received the popup
// The if statement will only evaluate to true if the popup is *not* explicitly disabled (that is, it is set to 1, or not set)
if (getCookie('bShowPopup') !== '0')
{
    var pctg = 60; // Set percentage here
    var rndm = Math.ceil(Math.Random() * 100);

    if (rndm <= pctg) {
        // Execute popup statement(s)
        //stmt123....

        // Set cookie bShowPopup to 0 (so the user won't receive popups ever again)
        // NOTE: The cookie will expire after 30 days.
        setCookie('bShowPopup', '0', 30);
    }
}
if (getCookie('bShowPopup') !== '0')
{
    var pctg = 60; // Set percentage here
    var rndm = Math.ceil(Math.Random() * 100);

    if (rndm <= pctg) {
        // Execute popup statement(s)
        //stmt123....
    }

    setCookie('bShowPopup', '0', 30);
}