Craig Verrill
Craig Verrill

Reputation: 3

Javascript not executed

I apologize in advance for asking such a silly question, but I can't seem to get my Javascript to be executed. I switched from my complete page to a simple example to make sure I wasn't doing anything too tricky, and I still can't get the JS to work. I have the following code saved in a file named index.html on my server:

     <%@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>

And what I see when I go to the page is :

 Hello World
 Still Hello World

In the Index.jsp file, I have:

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

I know the solution is insanely simple, but I haven't been able to come up with the line of code that fixes this. I've been copying example code over to my page just to see if somebody else's JS is executed properly, and I haven't gotten that either. Pretty sure the server runs Tomcat.

Thanks,

Craig

Upvotes: 0

Views: 697

Answers (2)

Stephen P
Stephen P

Reputation: 14830

out.println() is Java / JSP, it is not Javascript.

Try replacing it with 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>

Upvotes: 3

Borniet
Borniet

Reputation: 3546

Could it be that you're mixing up Java and JavaScript? They have totally different syntax. out.println is Java, to write to the document (output to the browserwindow), you would use document.write('STRING'); in JavaScript. Once you have an error like this in your JavaScript, the remainder of the JavaScript will not be executed either.

Upvotes: 0

Related Questions