Tuesday 9 March, 2010

Master Page - Content Page - Javascript Path problem


Most of the developers who are using Master page and content pages in different folders, paths has faced a problem of reference of javascript file.

It was a nightmare and was a big headache to decide whether to use the masterfile concept or not. There are many advantages of using master pages and content pages.
We will not go in detail to find the advantage but will look to find the solution for the javascript path issue. One major advantage of using Masterpage is, if we are using single masterpages for the content pages irrespective of any path of content pages, the browser will cache the javascripts and css files, which optimizes the performance of the website.

* To show in real life we created a appllication where we saved all the javascripts in a folder
[ ApplicationName]/Common/Scripts

Created a master page at [ ApplicationName]/Common/Usercontrols
   and dragged the javascript files .

Created two content pages one at 
      >>  [ ApplicationName]/Common/Usercontrols/Test.aspx and
      >>  [ ApplicationName]/Test.aspx
   refferring to the same master page.




While testing both,  [ ApplicationName]/Test.aspx was not reffered to the javascript, same will happen if any other pages which are in other path. This is the problem 


The solution is simple:
   Instead of

< script type="text/javascript" src='../Scripts/jQuery/jquery-1.3.2.min.js'>&lt/script>
 make the reference as
< script type="text/javascript" src=''<%=ResolveUrl("../Scripts/jQuery/jquery-1.3.2.min.js")%>   </script>


as shown below.

just add "ResolveUrl" for the src of the file