2016-07-02 2 views
1

이 코드는 페이지가 처음로드 될 때만 작동합니다.첫 페이지에 Javascript를 실행하십시오 load

어쨌든 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

스토어 첫 페이지로드에서 쿠키의 변수와 변수의 값마다 시간을 확인.

쿠키 사용에 대해서는 this을 참조하십시오.

은 [업데이트]]

나는 demo에 대한 HTML을 생성 인해 쿠키의 origin-same policy에 당신을위한 source.

소스를 다음 백업 코드로 복사했습니다. 앞에서 언급 한대로 origin-same 정책을 사용하므로 다음 코드를 자신의 원본에서 실행해야합니다.

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

자바 스크립트 코드를 복사했습니다. – harel486

+0

* 데모 *를 참조하십시오. 첫 페이지로드시 처음 **이 페이지가로드 됨 **이 표시되고 페이지를 새로 고침 한 후에 **이 페이지가로드되었습니다. **가 표시됩니다. Cookie를 다시 설정하여 데모를 다시 표시 할 수 있습니다. – Anson

0

스크립트가 처음로드 될 때만 실행되도록 100 % 확신 할 수는 없습니다.

"보안"을 실제로 원할 경우 세션, 쿠키 및 localStorage에 데이터를 저장하려고 할 수 있습니다.

관련 문제