2013-09-01 48 views
0

我用下面的JavaScript。當它是HTML裏面寫文件,它的工作原理當JavaScript是在HTML中它的工作原理,當它外面不

<script type="text/javascript"> 
     var d=new Date(); 
     var ShowenYear = d.getFullYear(); 
     var Month = d.getMonth(); 
     var NextSeason = "Unknown Season"; 

     if (Month == 1 || Month == 2 || Month == 3) { 
      NextSeason = "Spring"; 
     } 
     else if (Month == 4 || Month == 5 || Month == 6) { 
      NextSeason = "Summer"; 
     } 
      else if (Month == 7 || Month == 8 || Month == 9) { 
       NextSeason = "Autumn"; 
      } 
       else if (Month == 10 || Month == 11 || Month == 12) { 
        NextSeason = "Winter"; 
        ShowenYear++; 
        } 

     document.getElementById("Season").innerHTML = NextSeason + " " + ShowenYear; 
    </script> 

這工作完全正常,但是當我創建複製它所有(完全一樣的JS文件只是沒有腳本標籤

然後叫像這樣

<script type="text/javascript" src="js/showdate.js" ></script> 

的文件時,它根本不起作用

感謝誰願意幫助

+3

這將有助於粘貼您的確切的HTML代碼,當您加載文件,以及確切的路徑到您的文件。另外,該文件是否正確加載?您可以通過開發人員工具或Chrome中的控制檯查看。 – Agis

+0

將'window.onload'中的所有代碼包裝到 – Cherniv

+0

的任何錯誤中?什麼意思:「它根本不起作用」? – Piotrek

回答

1
<script language="Javascript" type="text/javascript" src="js/showdate.js"></script> 

嘗試寫<head>標籤內這行代碼。然後添加<body onload="showdate();">,這樣每次頁面加載時都會調用該函數。

function showdate(){ 
    var d=new Date(); 
      var ShowenYear = d.getFullYear(); 
      var Month = d.getMonth(); 
      var NextSeason = "Unknown Season"; 

      if (Month == 1 || Month == 2 || Month == 3) { 
       NextSeason = "Spring"; 
      } 
      else if (Month == 4 || Month == 5 || Month == 6) { 
       NextSeason = "Summer"; 
      } 
       else if (Month == 7 || Month == 8 || Month == 9) { 
        NextSeason = "Autumn"; 
       } 
        else if (Month == 10 || Month == 11 || Month == 12) { 
         NextSeason = "Winter"; 
         ShowenYear++; 
         } 

      document.getElementById("Season").innerHTML = NextSeason + " " + ShowenYear; 
    } 
} 
+0

仍似乎沒有工作,讓我瘋狂 – Shmagic

+0

你給了正確的'src' –

+1

你是否刪除了showdate.js中的

相關問題