javascript 在不使用 body 标签中的 onload 属性的情况下自动弹出 onload

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5871326/
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-25 18:37:53  来源:igfitidea点击:

automatic onload pop-up without using the onload attribute in body tag

javascriptpopup

提问by dead 2

Could you please give some links? When i google all spam sites come up!

你能给一些链接吗?当我用谷歌搜索所有垃圾邮件网站时!

回答by T.J. Crowder

You can do this with the loadevent on the windowobject:

您可以使用对象load上的事件执行此操作window

DOM0 style:

DOM0 风格:

window.onload = function() {
    alert("But please don't.");
};

Or using DOM2 methods:

或者使用 DOM2 方法:

if (window.addEventListener) { // DOM2 standard
    window.addEventListener('load', handler, false);
}
else if (window.attachEvent) { // Fallback for older IE versions
    window.attachEvent('onload', handler);
}
function handler() {
    alert("But again, please don't.");
}

As they say over at http://pastie.org, please use this information in your quest to save humanity, not your evil plots to take over the world.

正如他们在http://pastie.org 上所说的那样,请在您寻求拯救人类的过程中使用这些信息,而不是您要控制世界的邪恶阴谋。

回答by Adrian Schmidt

It's not quite clear whether you just don't want to use the onload attribute, just not on the body element, or if you don't want to use the onload event at all.

目前还不清楚您是不想使用 onload 属性,只是不想在 body 元素上使用,还是根本不想使用 onload 事件。

T.J. Crowders answer gives some good examples for using event listeners, which is the best way to do it.

TJ Crowders 的回答给出了一些使用事件侦听器的好例子,这是最好的方法。

If for some reason you don't want to use the onload event at all, you could put a script tag in the HTML just before the tag, with an alert inside it. You shouldn't do that for anything in production though... and it's not going to do the exact same thing as body.onload...

如果由于某种原因您根本不想使用 onload 事件,您可以在 HTML 标签之前放置一个脚本标签,并在其中添加一个警报。但是,您不应该对生产中的任何事情这样做......而且它不会做与 body.onload 完全相同的事情......

回答by dead 2

I got the code!

我得到了代码!

I am copy pasting the exact code which worked......

我正在复制粘贴有效的确切代码......

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js" type="text/javascript"></script>   
<script src="http://dinhquanghuy.110mb.com/jquery.cookie.js" type="text/javascript"></script>
<script type="text/javascript">
         var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
    centerPopup();
    //loads popup only if it is disabled
    if(popupStatus==0){
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#popupContact").fadeIn("slow");
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup(){
    //disables popup only if it is enabled
    if(popupStatus==1){
        $("#backgroundPopup").fadeOut("slow");
        $("#popupContact").fadeOut("slow");
        popupStatus = 0;
    }
}

//centering popup
function centerPopup(){
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;  
    var windowHeight = document.documentElement.clientHeight;  
    var windowscrolltop = document.documentElement.scrollTop; 
    var windowscrollleft = document.documentElement.scrollLeft; 
    var popupHeight = $("#popupContact").height();
    var popupWidth = $("#popupContact").width();
    var toppos = windowHeight/2-popupHeight/2+windowscrolltop;
    var leftpos = windowWidth/2-popupWidth/2+windowscrollleft;
    //centering
    $("#popupContact").css({
        "position": "absolute",
        "top": toppos,
        "left": leftpos
    });
    //only need force for IE6

    $("#backgroundPopup").css({
        "height": windowHeight
    });

}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
    if ($.cookie("anewsletter") != 1) {    

        //load popup
        setTimeout("loadPopup()",5000);    
    }        
    //CLOSING POPUP
    //Click the x event!
    $("#popupContactClose").click(function(){
        disablePopup();
        $.cookie("anewsletter", "1", { expires: 7 });
    });
    //Click out event!
    $("#backgroundPopup").click(function(){
        disablePopup();
        $.cookie("anewsletter", "1", { expires: 7 });
    });
    //Press Escape event!
    $(document).keypress(function(e){
        if(e.keyCode==27 && popupStatus==1){
            disablePopup();
            $.cookie("anewsletter", "1", { expires: 7 });
        }
    });

});
</script>