2012-08-07 71 views
0

我很抱歉提出這樣一個愚蠢的問題,但我似乎無法讓我的Javascript被執行。我從完整的頁面切換到一個簡單的例子,以確保我沒有做任何棘手的事情,我仍然無法讓JS工作。我已經保存在一個文件下面的代碼名爲我的服務器上的index.html:Javascript未執行

 <%@page import="index.jsp" %> 
    <html> 
     <head> 
      <title>Hello World</title> 
     </head> 
     <body> 
      Hello World!<br/> 
      <script type="text/JavaScript" src="index.jsp"> 
      </script> 
      Still hello world! 
      <script type="text/JavaScript"> 
       out.println(" Your IP address is " + request.getRemoteAddr()); 
       alert("Welcome to the boost converter site"); 
      </script> 
     </body> 
    </html> 

而我所看到的,當我去的網頁是:

Hello World 
Still Hello World 

在index.jsp文件,我有:

function InitPage(){ 
    out.println("Your IP address is " + request.getRemoteAddr()); 
} 

我知道解決的辦法是出奇的簡單,但我一直沒能拿出的代碼來修復此行。我一直在將示例代碼複製到我的頁面,以查看其他人的JS是否正確執行,但我也沒有得到。很確定服務器運行Tomcat。

感謝,

克雷格

+0

試試這裏的示例代碼http://www.w3schools.com/js/js_functions.asp – 2012-08-07 19:29:06

回答

3

out.println()是Java/JSP的,它不是的JavaScript。

嘗試用document.write('Your IP address is unknown');

<body> 
    Hello World!<br/> 
    <!-- Remove this. A .jsp is not Javascript. --> 
    <!--script type="text/JavaScript" src="index.jsp"> 
    </script--> 
    Still hello world! 
    <script type="text/javascript"> 
     /* You can't use request.getRemoteAddr() in Javascript. It's Java. */ 
     document.write("Your IP address is unknown");//+ request.getRemoteAddr()); 
     alert("Welcome to the boost converter site"); 
    </script> 
</body> 
+0

非常感謝。 Document.write是我需要的。 – 2012-08-07 19:43:41

0

取代它會不會是你混淆了Java和JavaScript?他們有完全不同的語法。 out.println是Java,要寫入文檔(輸出到browserwindow),可以使用document.write('STRING');在JavaScript中。 一旦在JavaScript中出現類似的錯誤,JavaScript的其餘部分也不會執行。

+0

是的,正是發生了什麼 - 聽起來noob警報。謝謝您的幫助。 – 2012-08-07 19:44:44