0

I tryed to include jquery to the Master Page ->

Here is body:

 <body onload="X()">
    <script type="text/javascript">
    .........
      function X() {
          alert('Trace');
          var s = $(window).width();
          alert(s);
      }
   </script>
 </body>

Here is head:

      <script type="text/javascript" src="~/Scripts/jquery-1.8.0.min.js"></script>

Browser cant resolve [$] symbol.I saved .aspx page that uses master ( [root][clear][My pages] ) as html with images and try open Jquery file -> I got eror:

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /clear/~/Scripts/jquery-1.8.0.min.js

2 Answers2

4

You need to include jquery js file before you use it. Add jQuery in head and use it before ending tag of body or in document.ready.

In head put it like this and remove ~.

<script type="text/javascript" src="/Scripts/jquery-1.4.1.js"></script>

In html before closing of body tag

<script type="text/javascript">
         function X() {
             var s = $(window).width();
             alert(s);
         }
</script>
</body>
Adil
  • 146,340
  • 25
  • 209
  • 204
  • http://stackoverflow.com/questions/731407/proper-way-to-use-jquery-when-using-masterpages-in-asp-net –  Dec 25 '12 at 09:14
  • I dont see the difference - onload="X()" is the same as your approach –  Dec 25 '12 at 09:20
  • First you are not registering jQuery in head but you are registering in body. Second you can using jQuery in head and jQuery will not be available to it. You are interchanging the position of jquery registration and its usage. – Adil Dec 25 '12 at 09:24
  • I followed your approach but still have eror.My be it is my own bug. –  Dec 25 '12 at 09:32
  • 1
    What error you are getting, Why you are using very old version of jQuery, jQuery 1.8.3 is available, http://jquery.com/download/ – Adil Dec 25 '12 at 09:35
  • Remove onload="X()" and call it after your function declaration. – Adil Dec 25 '12 at 09:56
  • I find somethin strange (soon I'll update question) My .aspx page on which I test master page is not in root - its in folder [clear].When I save it as html with images and tryed open jquery file I see eror:cant request the following URL:/clear/~/Scripts/jquery-1.8.0.min.js –  Dec 25 '12 at 09:59
  • I think you caught the bug – Adil Dec 25 '12 at 10:01
  • @AndyF find solution.shold we clear comments or they may be usefull somebody? –  Dec 25 '12 at 10:12
2

You'll need to resolve the tilde (~) in your path, like so: <script type="text/javascript" src="<%= Page.ResolveUrl("~/Scripts/jquery-1.8.0.min.js") %>"></script>

The path is then resolved by the server.

Andy F
  • 1,517
  • 2
  • 20
  • 35