Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.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 - Fatal编程技术网

Javascript 著名的弹出式挑战

Javascript 著名的弹出式挑战,javascript,Javascript,我使用此脚本将任何用户单击我的页面时重定向到我的youtube频道,但我希望它激活此脚本,或者在每次重新加载时在第一次单击我的页面时重定向用户。不是每次用户单击时,他都将重定向到我的频道 <script type="text/javascript"> document.body.onclick= function() { window.open('https://www.youtube.com ', 'poppage', 'toolbars=0, scrollbars=1, l

我使用此脚本将任何用户单击我的页面时重定向到我的youtube频道,但我希望它激活此脚本,或者在每次重新加载时在第一次单击我的页面时重定向用户。不是每次用户单击时,他都将重定向到我的频道

<script type="text/javascript">

document.body.onclick= function()

{

window.open('https://www.youtube.com ', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
}</script>

document.body.onclick=function()
{
打开窗户https://www.youtube.com “,”弹出窗口“,”工具栏=0,滚动条=1,位置=0,状态栏=0,菜单栏=0,可调整大小=1,宽度=650,高度=650,左侧=300,顶部=50';
}
使用此

document.body.onclick= function()
{  
window.open('https://www.youtube.com ', 'poppage', 'toolbars=0,scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
document.body.onclick = undefined;
}
请注意,在第一次执行代码后,我通过为onclick事件处理程序分配未定义的来删除它

document.body.onclick= function()
{  
window.open('https://www.youtube.com ', 'poppage', 'toolbars=0,scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
document.body.onclick = undefined;
}
var handler = function() {
  window.open('https://www.youtube.com ', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
  document.body.removeEventListener('click', handler);
};

document.body.addEventListener('click', handler);
请注意,在第一次执行代码后,我将未定义的赋值给onclick事件处理程序,从而删除了它

var handler = function() {
  window.open('https://www.youtube.com ', 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=1, width=650, height=650, left = 300, top = 50');
  document.body.removeEventListener('click', handler);
};

document.body.addEventListener('click', handler);
使用
addEventListener
removeEventListener
管理事件处理程序是一种更好的做法。通过这样做,附加到
onclick
的其他事件处理程序不会被覆盖


使用
addEventListener
removeEventListener
管理事件处理程序是一种更好的做法。通过这样做,附加到
onclick
的其他事件处理程序不会被覆盖。

Sweet!我得记住,亲爱的!我要记住这一点。