自动加载弹出,不使用主体标签中的onload属性

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

本文关键字:标签 主体 属性 onload 加载      更新时间:2023-09-26

你能给一些链接吗?当我谷歌所有的垃圾网站出现!

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

DOM0)风格:

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

或者使用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.");
}

正如他们在http://pastie.org上所说,请使用这些信息在你的任务中拯救人类,而不是你的邪恶阴谋接管世界。

你是不想在body元素上使用onload属性,还是根本不想使用onload事件,这还不是很清楚。

T.J.克劳德的回答给出了一些使用事件监听器的好例子,这是最好的方法。

如果出于某种原因你根本不想使用onload事件,你可以在HTML标签的前面放一个script标签,里面有一个警告。但是在生产环境中你不应该这样做……它不会像body。onload。

那样

我拿到代码了!

我正在复制粘贴工作的确切代码......

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