在页面显示之前加载 Javascript 警报

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12280575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:48:36  来源:igfitidea点击:

Javascript alert loads before page displays

javascripthtmlsafari

提问by mjroodt

On my mobile, in safari If I go to my default page that has alert("Hello")on the body onload event, the alert displays with my default page fully visible in the background. If I then go to another site for example bbc.co.uk and then type in my the web address for my default page in the address bar, the alert shows with the BBC content in the background, its like the alert loads before the page has loaded.

在我的手机上,在 safari 中如果我转到具有alert("Hello")主体 onload 事件的默认页面,警报会显示,我的默认页面在后台完全可见。如果我然后转到另一个站点,例如 bbc.co.uk,然后在地址栏中输入我的默认页面的网址,则警报会在后台显示 BBC 内容,就像警报在页面之前加载一样已加载。

How do I only show the message once the whole page is visible. I've read that window.onloadwaits until everything is loaded before it triggers the alert but I must be getting something wrong because the behaviour doesn't change. I've also tried:

如何仅在整个页面可见后才显示消息。我读过它window.onload会等到所有东西都加载后才会触发警报,但我一定是出了什么问题,因为行为没有改变。我也试过:

$(document).ready(function () {
    window.onload= alert('Test');
});

and

<meta http-equiv="Pragma" content="no-cache"/>

in case it has something to do with cache but I don't think this is the issue. Any ideas ?

以防它与缓存有关,但我认为这不是问题。有任何想法吗 ?

Thanks

谢谢

采纳答案by Mihai Iorga

$(window).load(function() {
      alert('Test');
});

回答by Clyde Lobo

You pass a reference to a function to the window.onloadand not the actual call.

您将对函数的引用传递给window.onload而不是实际调用。

try

尝试

window.onload = function(){
 alert('test');
}

回答by Pranay Rana

if you wanto display the alrert box either use window.onload there is no point in use both , here is code that will work fine

如果您想显示警报框,请使用 window.onload 两者都没有意义,这里是可以正常工作的代码

window.onload(which is implemented even in old browsers), which fires when the entire page loads

window.onload(即使在旧浏览器中也已实现),在整个页面加载时触发

 window.onload = function(){  alert('test'); } 

jQuery provides document.ready, which abstracts those away, and fires as soon as the page's DOM is ready

jQuery 提供 document.ready,它将那些抽象掉,并在页面的 DOM 准备好后立即触发

$(document).ready(function () {     
 alert('Test');  
});

Check answer from : window.onload vs $(document).ready()

检查答案:window.onload vs $(document).ready()

window.onloadis the built-in Javascript event, but as its implementation had subtlequirks across browsers (FF/IE6/IE8/Opera), jQuery provides document.ready, which abstracts those away, and fires as soon as the page's DOM is ready (doesn't wait for images etc.).

window.onload是内置的 Javascript 事件,但由于它的实现在浏览器 (FF/IE6/IE8/Opera) 之间有微妙的怪癖,jQuery 提供了 document.ready,它将这些抽象出来,并在页面的 DOM 准备好后立即触发(不'不要等待图像等)。

document.readyis a jQuery function, wrapping and providing consistencyto the following events:

document.ready是一个 jQuery 函数,包装并为以下事件提供一致性

  • document.ondomcontentready/ document.ondomcontentloaded- a newish event which fires when the document's DOM is loaded (which may be some time beforethe images etc. are loaded); again, slightly different in IE and in rest of the world
  • and window.onload (which is implemented even in old browsers), which fires when the entire page loads (images, styles, etc.)
  • document.ondomcontentready/ document.ondomcontentloaded- 加载文档的 DOM 时触发的新事件(可能在加载图像等之前的一段时间);再次,在 IE 和世界其他地方略有不同
  • 和 window.onload(即使在旧浏览器中也已实现),它会在整个页面加载(图像、样式等)时触发

回答by rehanoor

<!DOCTYPE html>
<html>
<body onload="show_popup()">

<script>
function show_popup() {
    alert("Popup shows");
}
</script>

</body>
</html>