javascript - 仅在首页加载时运行 Javascript

标签 javascript asp.net

我希望此代码仅在页面第一次加载时起作用。

有没有办法在 javascript 上不使用 IsPostBack

IsPostBack :获取一个值,该值指示页面是首次呈现还是正在加载以响应回发。 More here .

<script>  

window.onload = function TimedCss()
{

    setTimeout(myTimeout1, 0500) 
    setTimeout(myTimeout2, 1000) 
    setTimeout(myTimeout3, 1500)
    setTimeout(myTimeout4, 2000) 
    setTimeout(myTimeout5, 2500)
    setTimeout(myTimeout6, 3000)
}



}
function myTimeout1() 
{
    document.getElementById("LBLName").className = " animated fadeInLeft";
    document.getElementById("LBLName").style.visibility = "visible";
}
function myTimeout2() 
{ 
    document.getElementById("LBLDescription").className = " animated rotateIn";
    document.getElementById("LBLDescription").style.visibility = "visible"; 
}
function myTimeout3() 
{
    document.getElementById("P1").className = " animated zoomIn";
    document.getElementById("P1").style.visibility = "visible";
}
function myTimeout4()
{
    document.getElementById("TXTQuantity").className = " animated flipInY";
    document.getElementById("TXTQuantity").style.visibility = "visible";
}
function myTimeout5()
{
    document.getElementById("LBLPrice").className = " animated slideInLeft";
    document.getElementById("LBLPrice").style.visibility = "visible";
}
function myTimeout6()
{
    document.getElementById("BTNAddToCart").className += " animated fadeInUp";
    document.getElementById("BTNAddToCart").style.visibility = "visible";
}

</script> 

编辑 - 解决方案:

<script>
window.onload = function TimedCSS()
{
    var isPostBack=<%= IsPostBack ? "true" : "false" %>

    if (!isPostBack)
    {
        setTimeout(myTimeout1, 0500) 
        setTimeout(myTimeout2, 1000) 
        setTimeout(myTimeout3, 1500)
        setTimeout(myTimeout4, 2000) 
        setTimeout(myTimeout5, 2500)
        setTimeout(myTimeout6, 3000)
    }




function myTimeout1() 
{
    document.getElementById("LBLName").className = " animated fadeInLeft";
    document.getElementById("LBLName").style.visibility = "visible";
}
function myTimeout2() 
{ 
    document.getElementById("LBLDescription").className = " animated rotateIn";
    document.getElementById("LBLDescription").style.visibility = "visible"; 
}
function myTimeout3() 
{
    document.getElementById("P1").className = " animated zoomIn";
    document.getElementById("P1").style.visibility = "visible";
}
function myTimeout4()
{
    document.getElementById("TXTQuantity").className = " animated flipInY";
    document.getElementById("TXTQuantity").style.visibility = "visible";
}
function myTimeout5()
{
    document.getElementById("LBLPrice").className = " animated slideInLeft";
    document.getElementById("LBLPrice").style.visibility = "visible";
}
function myTimeout6()
{
    document.getElementById("BTNAddToCart").className += " animated fadeInUp";
    document.getElementById("BTNAddToCart").style.visibility = "visible";
}

</script>

最佳答案

在首次页面加载时将变量存储在 Cookie 中,并每次检查该变量的值。

引用this用于 Cookie 的使用。

[已更新]

我为 demo 创建了一个 html和 source为您服务 origin-same policy Cookie。

我将源代码复制到以下代码中进行备份。您必须在自己的源上执行以下代码,因为前面提到的源相同政策。

<!DOCTYPE>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    </head>
    <body>
        <div id="pageLoadStatus"></div>
        <script>
            var _ = {};
            /**
             * Gets or sets cookies
             * @param name
             * @param value (null to delete or undefined to get)
             * @param options (domain, expire (in days))
             * @return value or true
             */
            _.cookie = function(name, value, options)
            {
                if (typeof value === "undefined") {
                    var n, v,
                        cookies = document.cookie.split(";");
                    for (var i = 0; i < cookies.length; i++) {
                        n = $.trim(cookies[i].substr(0,cookies[i].indexOf("=")));
                        v = cookies[i].substr(cookies[i].indexOf("=")+1);
                        if (n === name){
                            return unescape(v);
                        }
                    }
                } else {
                    options = options || {};
                    if (!value) {
                        value = "";
                        options.expires = -365;
                    } else {
                        value = escape(value);
                    }
                    if (options.expires) {
                        var d = new Date();
                        d.setDate(d.getDate() + options.expires);
                        value += "; expires=" + d.toUTCString();
                    }
                    if (options.domain) {
                        value += "; domain=" + options.domain;
                    }
                    if (options.path) {
                        value += "; path=" + options.path;
                    }
                    document.cookie = name + "=" + value;
                }
            };

            var hasLoadedBefore = _.cookie('hasLoadedBefore');
            if(!!hasLoadedBefore) $('#pageLoadStatus').text('This page has been loaded before.');
            else $('#pageLoadStatus').text('This page loaded at first time.');
            _.cookie('hasLoadedBefore', true);
        </script>
    </body>
</html>

关于javascript - 仅在首页加载时运行 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38159371/

相关文章:

javascript - 在 Node 中递归地请求模块

javascript - 绕过一个特定 div JS(iOS)上的文档滚动锁定

asp.net - 在应用程序之间传递 Azure AD 身份验证

asp.net - Ajax htmlEditorExtender - 无法识别的元素清理程序

asp.net - CSS 表单、ASP.NET 验证摘要是靠不住的

javascript - 确认删除提醒

javascript - 有关电话号码的信息

javascript - 带有生成字段的 Ng-repeat 提交表单

javascript - 从数据库中获取嵌套 JSON 的最佳方法是什么?

asp.net 通过 kerberos 集成 windows 身份验证到 sql server