2017-03-29 35 views
0

當前我在前端編程,並遇到一個問題,我的JavaScript文件不加載。我的文件是scala/scalatra後端的.jsp版本。我有我的index.jsp,其中我的頭文件包含在頂部和底部,包括我的頁腳。JavaScript文件未加載到.jsp文件中

我有他們這樣的:

<jsp:include page="header.jsp"/> 
// CODE // 
<jsp:include page="footer.jsp"/> 

我的頭看起來是這樣的:

<!-- HEADER --> 
<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title></title> 
    <link rel="stylesheet" href="styles/cssfile.css"> 
    <link rel="stylesheet" href="css/cssfile.css"> 
</head> 
<body> 

我的頁腳是這樣的:

<!-- FOOTER --> 
     <script type="text/javascript" src="js/jquery.js"></script> 
     <script type="text/javascript" src="js/main.js"></script> 
</body> 
</html> 

我main.js樣子這個:

$(document).ready(function() { 
'use strict'; 
$(function() { 

    $('a[href*=#]:not([href=#])').click(function() { 

      if (
        location.pathname.replace(/^//,'') == this.pathname.replace(/^//,'') 
       || location.hostname == this.hostname 
      ) { 

      var target = $(this.hash); 

      target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 

      if (target.length) { 

        $('html,body').animate({ 
        scrollTop: target.offset().top 
        }, 1000); 
        return false; 

      } 

      } 

    }); 

}); 

});

我的文件結構是這樣的:

css 
    -cssfile1 
    -cssfile2 
js 
    -jquery.js 
    -main.js 
header.jsp 
index.jsp 
footer.jsp 

先生SRC路線shoulde是正確的,當我insepect網站的源代碼,我可以看到main.js文件,並點擊它,它是正確的js。 我試過console.log(「test」);並沒有顯示,所以它不會加載。 我添加了jquery.js並在其中添加了jquery javascript,並在頁腳中添加了它,正如你所看到的。

所以我想要的是我的main.js應該加載。

感謝提前:)

+0

實際發送到瀏覽器的結果HTML是什麼?在瀏覽器的調試工具中,是否需要創建'.js'文件?服務器的迴應是什麼?在你的'.js'中是否有另一個錯誤阻止'console.log(「test」);'執行?顯示足夠的代碼來複制問題。 – David

回答

0

JSP EL提供了可以在設計時可以用來幫助解決在運行時正確的路徑變量($ {pageContext.request.contextPath})。試試這可能會有幫助,你使用正確的jQuery庫嗎?

<script type="text/javascript" src="${pageContext.request.contextPath}/js/main.js"></script> 


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"> 
+0

它沒有爲我工作,我的JavaScript仍然不工作,我沒有得到與console.log(「測試」)的迴應; – code4fun

+0

您是否使用正確的jquery我爲您添加了CDN嘗試 –

+0

這就是我現在在我的頁腳中,但我仍然沒有工作。在控制檯中,我得到:GET http:// localhost:8080/js/main.js作爲404 – code4fun