2016-07-02 110 views
1

我希望此代碼僅在第一次加載頁面時工作。僅在第一頁加載運行Javascript

反正是有關於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> 
+2

除了填充文本,你可以解釋'IsPostBack'是什麼。 – JJJ

+0

@Juhana [的IsPostBack ...](https://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback(V = vs.110)的.aspx) – harel486

+2

'0500'是順便說一下,320。 – Xufox

回答

1

商店在第一頁加載中的Cookie一個變量,每次檢查變量的值。

參考this對Cookie的使用情況。

[增訂]

我創建了一個HTML爲demo和因餅乾的origin-same policysource你。

我複製源到作爲備用下面的代碼。您必須在您自己的原產地執行以下代碼,因爲原產地 - 與前面提到的相同的政策

<!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> 
+0

我已經複製了JavaScript代碼,現在呢? – harel486

+0

請參閱* demo *。你會看到**第一次加載這個頁面。**在第一次加載頁面時,你也會看到**這個頁面在你重新加載頁面之前已經被加載。您可以通過重置Cookie來重新顯示演示。 – Anson

0

請注意,您不能100%確定您的腳本只能在第一次加載時運行。

如果你真的想爲「安全」,你可以嘗試存儲在會話,餅乾和localStorage的數據。

相關問題