How do you include JavaScript in MasterPage?
You can include a .js file either between the head tags , contentplaceholder tags or inside the body tags. This will in all cases be reflected in your other pages that include this masterpage. All you need to focus on is the way that the path is created.
The code below adds a jquery file to a masterpage in the head section of the masterpage.
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<title></title>
<script src="jquery-2.1.1.min.js"></script>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
<script>
</script>
Relative vs Absolute URL's
By using ../ and ~/ before the url path , you are creating a relative URL. The paths of relative URL's is affected when you change the folder level of either the file that you are referring to or the file which contains the link.
../ symbol make one step out of the folder containing the link. make sure you have enough '../' to refer to the correct file.
~/ symbol creates a path that starts at the root of your project.
To create an absolute URL ,Just drag the file you intend to include in the page from solution explorer in Visual Studio to the page.
For more about the difference between absolute and relative URL's check Difference between Relative path and absolute path in javascript
Try replacing ~/ with ../. One of my projects was doing the same thing and that fixed it.
Also, make absolutely certain that even on the server (and not just in the project), the JS folder is directly below the root.
HTML
You typically don't want any scripts in the <head />
apart from scripts like Modernizr that have feature detection. It's more of a best practice to move all scripts to the bottom of the page like so:
<html>
<head runat="server">
<title></title>
<link rel="stylesheet" href='<%= ResolveUrl("~/css/style.css") %>' />
<asp:ContentPlaceHolder ID="Head" runat="server" />
</head>
<body>
<!-- Scripts at bottom of page for faster loading. -->
<script src='<%= ResolveUrl("~/js/jquery-1.7.1.min.js") %>'></script>
<script src='<%= ResolveUrl("~/js/script.js") %>'></script>
</body>
</html>
SCRIPT.JS
Referencing the other script files in script.js will require the /
to be appened to 'js/` like so:
$.include('/js/superfish.js');
$.include('/js/FF-cash.js');
$.include('/js/tms-0.4.x.js');
$.include('/js/uCarausel.js');
$.include('/js/jquery.easing.1.3.js');
$.include('/js/jquery.tools.min.js');
$.include('/js/jquery.jqtransform.js');
$.include('/js/jquery.quicksand.js');
$.include('/js/jquery.snippet.min.js');
$.include('/js/jquery-ui-1.8.17.custom.min.js');
$.include('/js/jquery.cycle.all.min.js');
$.include('/js/jquery.cookie.js');
if($('.tweet').length)
$.include('/js/jquery.tweet.js');
if($('.lightbox-image').length)
$.include('/js/jquery.prettyPhoto.js');
if($('#contact-form').length || $('#contact-form2').length)
$.include('/js/forms.js');
if($('.kwicks').length)
$.include('/js/kwicks-1.5.1.pack.js');
if($('#counter').length)
$.include('/js/jquery.countdown.js');
if($('.fixedtip').length || $('.clicktip').length || $('.normaltip').length)
$.include('/js/jquery.atooltip.pack.js');
// Slider
$('.main-slider')._TMS({
MISC
Don't forget to clear your cache or work in private browsing while testing all of this!