javascript - 随机无右键单击消息不起作用

标签 javascript

所以我们都知道,一旦发布图像,您就无法真正保护它们,但取笑外行人还是很有趣的。我添加了一个脚本来防止右键单击,并试图随机化有趣的消息......它不起作用。

脚本:

// BACKGROUND IMAGES
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}



// no right click
var message=["That doesn't belong to you!  Put the mouse down and no one gets hurt!";
             "Oh, you again.  We of the internet have chosen to defy you!";
         "How would you like it if I walked into your house and tried to help myself to     your furniture?";
         "Hey that tickles!"
         "Thief!  You are being directed to the...  nah just kidding. Enjoy!"]


function clickIE4(){
    if (event.button==2){
        alert(message);
        return false;
    }
}

function clickNS4(e){
    if (document.layers||document.getElementById&&!document.all){
        if (e.which==2||e.which==3){
            alert(message);
            return false;
        }
    }
}

if (document.layers){
    document.captureEvents(Event.MOUSEDOWN);
    document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
    document.onmousedown=clickIE4;
}

document.oncontextmenu=new Function("alert(message);return false")

我忘记了什么?看起来应该可以。当我删除 [ 和 ] 并将其减少为一条消息时,该消息工作得很好。当我添加额外的内容并尝试将其随机化时,就会出现问题。

最佳答案

您正在向整个阵列发出警报。
如果您想要随机消息,您可以执行以下操作:

 var rn = Math.floor(Math.random() * message.length);
 alert(message[rn]);

这会根据数组中的消息数生成一个随机数。

Math.random
Math.floor

关于javascript - 随机无右键单击消息不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21053224/

相关文章:

javascript - 何时何地添加点击处理程序?

javascript - 如何从异步调用返回响应?

javascript - 使用我的 if 语句更精确

javascript - 使用handlebar.js循环

javascript - 如何从 C# 中的代码隐藏动态生成 JSON 对象

javascript - 根据条件 ng-change 更改 ng-model 字段的值

Javascript 导入函数语法

javascript - 打开注释器(注释存储在哪里?)

javascript - 链接到 Javascript 来源的内容

javascript - 如何使用javascript在浏览器中创建http服务器?