views:

697

answers:

4

Hello!

I have searched this web looking for an answer, but it seems that this time I'm not so lucky, so I am forced to ask. I apologize if it's already answered (could not find it). And yes, English is not my first language, so I also apologize for my spelling mistakes, I try my best.

This is my problem, using Tomcat 5.5, Struts 1.3, JRE 1.5 and I'm using firefox 3.5.6. In my jsp page I cannot seem to put any src="path/path" in my <script> I have tried deleting the src and all works well, but my project is going to need a lot of use from jquery and I do not want to copy/paste all the js file in every jsp.

This is my code:

<script type="text/javascript" src="js/jquery-1.3.2.js">
    function showMySelf(){
        alert("Hello World!");      
    }
(... plus other stuff code that actually uses jquery functions)
</script>

and the submit button:

<input type="submit" onclick="showMySelf()">

When I click the button, nothing happens (well it actually repaints the page) and when I delete the "src" tag from the script and add all the jquery code to the page it all works well.

I have tried putting another slash in the path as "/js/jquery-1.3.2.js" and returns an error.

I have tried using ResolveURL and it doesn't seem to give me better results.

I have also tried changing the js file to another file ("generics.js" and "js.js"), I also tried with "js/*.js".

Any of theese solutions have archived anything.

I have also tried using the struts tags (like html:submit) but it also did not work.

The path is actually right, since looking the code in my web browser gives me a link to the js file. So I suposse the browser knows were to look for my js file, it does not give me an error or a broken link to the file.

Any ideas of why this is happening?

Thank you all.

Random.

+1  A: 

I think Gumbo solved it.

As a sidenote, a very good way to find out whether a browser can load a JS file is the "Net tab" in Firebug in Firefox. It shows all loaded (and failed) requests of the current page.

Pekka
A: 

The two most likely options are:

a) You are including HTML in your JS file (i.e. <script> tags)

Take it out.

b) You have the wrong URI and when you attempt to resolve your relative URI manually you do so incorrectly

Look at your server access logs to see what is actually being requested (or use a tool such as Firebug)

David Dorward
A: 

The first thing to do in such case. Install Firebug and look at the "Console" panel (for possible syntax errors) and the "Net" panel to see whether your jQuery sources are being fetched correctly. The 2nd column there shows the request status code. alt text (full size image)

Juri
+2  A: 

You can not use a script element to load an external file and put code in it at the same time. You need to use two script elements:

<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
    function showMySelf(){
        alert("Hello World!");      
    }
(... plus other stuff code that actually uses jquery functions)
</script>
Gumbo
+1 well spotted, Sir.
Pekka
This worked. Thanks! I tried this solution earlier, but I closed the script with /> and not with ></scri... and didn't work so I suposed that that was not the answer. After installing the Firebug (thanks to all people for the suggestion!) and looking and trying the diferent replies I end up copy/past-ing in savage mode (AKA full of anger) and it seemed to magically work. After comparing the differences I found that the closing of the script tag is the only diference between the working code and the non-working code.So there it is. Again, thaks to all people :)
Random