最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Javascript alert loads before page displays - Stack Overflow

programmeradmin0浏览0评论

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.

How do I only show the message once the whole page is visible. I've read that window.onload waits 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:

$(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

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.

How do I only show the message once the whole page is visible. I've read that window.onload waits 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:

$(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

Share Improve this question edited Sep 5, 2012 at 11:43 takrl 6,4723 gold badges63 silver badges70 bronze badges asked Sep 5, 2012 at 11:37 mjroodtmjroodt 3,3135 gold badges25 silver badges35 bronze badges 2
  • Difference between $(document).ready and $(window).load in jQuery – smilly92 Commented Sep 5, 2012 at 11:50
  • The answer you accepted doesn't really solve the problem – Aragorn Commented Oct 7, 2018 at 20:35
Add a comment  | 

4 Answers 4

Reset to default 10

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

try

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

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 (which is implemented even in old browsers), which fires when the entire page loads

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

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

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

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

window.onload is the built-in Javascript event, but as its implementation had subtle quirks 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.).

document.ready is a jQuery function, wrapping and providing consistency to the following events:

  • document.ondomcontentready / document.ondomcontentloaded - a newish event which fires when the document's DOM is loaded (which may be some time before the 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.)
$(window).load(function() {
      alert('Test');
});

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

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

</body>
</html>

发布评论

评论列表(0)

  1. 暂无评论