Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't get jQuery to have effect

 
Mike London
Ranch Hand
Posts: 1208
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps this shows my newness to jQuery, but, like JavaScript, it seems difficult to get the paths set up correctly initially so that anything happens.

I have a JSP with the following paths in the head tag:


<script language="JavaScript" type="text/JavaScript"></script>
<script src="./jquery.js" type="text/javascript"></script>
<script src="./functions.js" type="text/javascript"></script>

<link rel="stylesheet" href="./test.css" type="text/css" media="screen" />

-------------

And, in the same directory, per the paths above, I have jquery.js, functions.js, and test.css.

Here are the contents of those (except for jquery.js) files:

test.css

#mainText { display: none; }

.title {
font-size: 2.5 em;
font-style: italic;
}

(the JSP file has both a div called mainText and a div id called title)
------------------------

functions.js

$(document).ready(function() {
$('#mainText').fadeIn('2000');
});

--------------------------

Any ideas why jQuery isn't doing anything? Probably simple mistake...

Thanks in advance.

Mike
 
Rory Slegtenhorst
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,

Try this:


Note that the "./" isn't neccesary if the js is next to the html.
Also note that you need the display style on the tag itself!

Hope this helps,
Rory
 
Mike London
Ranch Hand
Posts: 1208
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but this doesn't address the path problem I'm having.

Also, I named my jQuery file just jquery.js.

Thanks.

-- Mike
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a web application, it's a really bad idea to use page-relative URLs. Chances are, they are not addressing what you think they are since the URLs don;t generally reflect real forlders but are servlet mappings.

See the JSP FAQ on resource URLs to learn how to create proper server-relative URLs to your JavaScript files and other images.

P.S. One quick way to find out if jQuery is loaded is to do: alert(jQuery). And see what you get.
 
Mike London
Ranch Hand
Posts: 1208
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

Thanks as always for your reply.

I thought I was using the relative paths like an example you sent me a week or so back.

In any case, I have another problem since using the context path (from the FAQ you mentioned) has the same problem (can't find jQuery).

And just using the file name itself with no leading path (.\ or anything else) has the same problem too.

Hmmmmmm......

-- Mike
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the js file in relation to the web app root? Starting with the context path, it should be straight-forward.
 
Mike London
Ranch Hand
Posts: 1208
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

The problem turned out to be snytax, not paths.

I had a "." where I needed a "#" in the CSS (the JSP had an id not a class defined).

Thanks for your reply.

- Mike
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I had a dime for every time I made that kind of mistake...
 
Mike London
Ranch Hand
Posts: 1208
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic