views:

492

answers:

6

Is there any reason why JQuery would not work when used in an ASPX page? We have been trying to get working a simple call like this:

$(document).ready(function() {
    alert("This is SPARTA!!!!!!!!!");
});

An the alert box is never shown. I've had some experience with JS and ASP.Net before with buttons and I know that I have to return false after the JS like:

<input type=button value="Button 1" 
   onclick='button_1_onclick();return false;'>

Is there a similar thing to be done?

Edited: I have ran Firebug in the debugger mode and the JS is never ran, all the libs are present in the page. The only thing I can think of is that the ready event is not triggered or something similar.

+2  A: 

Are you using another library at the same time? The $ may have been re-assigned, try this:

jQuery(document).ready(function() {
    alert("This is SPARTA!!!!!!!!!");
});
Kieron
ups the ; was a typo, I edited the question.
mandel
+1  A: 

There could be two resons for this:

1) You might be missing the

<script language="JavaScript" src="JQuery.js"></script>

statement.

2) A semicolon is missing at the end of your alert. It should be:

$(document).ready(function() {   alert("This is SPARTA!!!!!!!!!"); });
Sachin Gaur
The semicolon was a typo, sorry.... I have updated the question with more info :D
mandel
+1  A: 

Have you tried putting the JavaScript reference after any CSS includes?

I have had problems where $(document).ready didn't run since the CSS classes had not been loaded. Moving the jQuery include seem to sort it out.

Might not be the same as your problem but it is worth a try.

Colin G
+4  A: 

ASP.NET and JQuery play really nicely together (and also with ASP.NET AJAX library too), I have used all of them together on a recent ASP.NET 3.5 web application. There are some oddities to be aware of, but on the whole work well together.

Firstly, you need to ensure that you have a script tag with src of your jQuery file-

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"&gt;&lt;/script&gt;

I have usually done this by adding a ScriptReference to the ScriptManager, but either way works. Then put your jQuery code after the script tag -

$(function() { alert("This is SPARTA!!!!!!!!!"); });

If your jQuery file is in a folder and you have some kind of authentication set up on your site, then you need to ensure that the folder is accessible on the pages where you are using jQuery, by modifying the web.config (much like you need to do with images and css). I've been tripped up by this before. You can check in firebug to see if the jQuery code is accessible (i.e. visible) by expanding the relevant script node; if you see some kind of HTTP error, then it could be due to the script not being accessible.

As others have already said, there could be conflicts with other JavaScript libraries. You can still use the $ shorthand if also using libraries by using the following pattern

(function($) {

$(function() { alert("This is SPARTA!!!!!!!!!"); });

})(jQuery);

This means that $ within the scope of the outer function is shorthand for jQuery object.

Russ Cam
+1  A: 

Maybe you have made the same mistake as me:

I included the jQuery script reference in my master page. The result was that the .Aspx page could not find the JQuery.js file, because I put it in a different folder.

In the masterpage I now include the jQuery, using

<script src="<%= this.ResolveUrl("~/js/jquery.js") %>"></script>
GvS
A: 

I tried this. You need to reference the jquery library and you need to have the $(document).ready function. You also need to reference your button with a class so that jquery can find which button you're looking for. You then tell jquery what you're going to do with that object, such as using .click().

Here is some code: Reference the jquery library.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"&gt;&lt;/script&gt;

Wrap the following in a script tag.

    $(document).ready(function()
    { 
        $('.clicker').click(function() 
        {

           alert('This is SPARTA!!!!');
        });
    });

Then in the HTML of the page you make your button like this:

<input type="button" value="Show Alert" class="clicker" />

Hope this helps some.