0

So I have a very simple .Net page that uses JQuery. When I place the code directly on the page the script executes fine, but when I call from an external js page it fails.

$(document).ready(function () {
    $("#MainContent_Button1").click(function () {
        alert("Hello world!");
    });

});
(jQuery)

<asp:Button ID="Button1" CssClass="Button1" runat="server" Text="CLICK ME FOO" />

When I link like so:

<script type="text/javascript" src="menu.js"></script>. 

It doesn't work, but if I place on the page it works fine.

8
  • what do you mean .. when i call it from an external page? Commented Sep 22, 2011 at 23:34
  • You mean when you have the $(function() {..}) in an external JS file? When do you load the external file? Commented Sep 22, 2011 at 23:35
  • Use the IE9, Chrome or Firefox Firebug console to troubleshoot. You haven't provided enough information to actually answer, but my guess is you're including the above jQuery code before you include jQuery itself. Commented Sep 22, 2011 at 23:36
  • @MoXplod When I link like so: <script type="text/javascript" href="menu.js"></script>. It doesn't work but if I place on the page it works fine. Commented Sep 22, 2011 at 23:39
  • @Dave Yes when the function is in an external JS file it bonks out. I know the function works as well as the direct path to it. I don't get it. But I've tried loading in the header and in the document. Commented Sep 22, 2011 at 23:40

2 Answers 2

1

It's supposed to be src="" in the script tag.

<script type="text/javascript" src="menu.js"></script>
Sign up to request clarification or add additional context in comments.

6 Comments

... or, the include itself is not right... (See comments below question.) Note, this is why I use Firebug to check to make sure the file is downloading.
Right. I mis-typed here but sure src="".
@Ricky - You're going to have to provide evidence for us to check. Use a console function in one of the browsers that support to make sure the file is downloading.
This is my entire code. <html xmlns="w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="ajax.googleapis.com/ajax/libs/jquery/1.4.2/…> </head> <body> <form id="form1" runat="server"> <div> <script type="text/javascript" src="menu.js"></script> <asp:Button ID="Button1" runat="server" Text="CLICK ME FOO" /> </div> </form> </body> </html> When I place the function where the link is it works fine.
@Ricky Maybe the question would be the best place to put that.
|
0

Most likely the javascript file you are referencing externally is not loading. Use chrome to make sure you are getting a 200 message for that file and not a 404. Also, make sure in your external javascript file you are including the jquery document load method exactly like you have above. You have to ensure the button has been placed in the DOM before you can bind any events to it. Also I would recommending using ClientIDMode="static" so you can reference the button with the actual id of button in the jQuery selector.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.