Problem with Javascript in Master Pages- Any Ideas?

Rate It (3)

Last post 02-07-2008 1:24 PM by SurojitBanerjee. 23 replies.

Sort Posts:

  • Re: Problem with Javascript in Master Pages- Any Ideas?

    11-27-2006, 4:43 PM
    • Loading...
    • Myroslav
    • Joined on 11-27-2006, 4:26 PM
    • London, UK
    • Posts 9

     You can't reference your files using "~"....
    Like you do here:

    <
    link rel="stylesheet" type="text/css" href="~/App_Theme/One.css"/>
    or here:
    <script type="text/javascript" src="~/App_Theme/corner.js"></script>

    The "~" to get root folder will work only for server side controls.
    I have similar problem and unfurtunately it appears there is no straight forward alternative to get relative path to your javascript/images/or css files. 

     

  • Re: Problem with Javascript in Master Pages- Any Ideas?

    11-27-2006, 4:49 PM
    • Loading...
    • Myroslav
    • Joined on 11-27-2006, 4:26 PM
    • London, UK
    • Posts 9

     You can't reference your files using "~"....
    Like you do here:

    <
    link rel="stylesheet" type="text/css" href="~/App_Theme/One.css"/>
    or here:
    <script type="text/javascript" src="~/App_Theme/corner.js"></script>

    The "~" to get root folder will work only for server side controls.
    I have similar problem and unfurtunately it appears there is no straight forward alternative to get relative path to your javascript/images/or css files. 

     

  • Re: Problem with Javascript in Master Pages- Any Ideas?

    11-27-2006, 4:50 PM
    • Loading...
    • Myroslav
    • Joined on 11-27-2006, 4:26 PM
    • London, UK
    • Posts 9

     You can't reference your files using "~"....
    Like you do here:

    <
    link rel="stylesheet" type="text/css" href="~/App_Theme/One.css"/>
    or here:
    <script type="text/javascript" src="~/App_Theme/corner.js"></script>

    The "~" to get root folder will work only for server side controls.
    I have similar problem and unfurtunately it appears there is no straight forward alternative to get relative path to your javascript/images/or css files. 

     

  • Re: Problem with Javascript in Master Pages- Any Ideas?

    11-27-2006, 7:30 PM

    Try using a site-root relative path like <link rel="stylesheet" type="text/css" href="/YourWebsite/App_Theme/One.css"/>

     

    Read more on MSDN: ASP.NET Web Site Paths

  • Re: Problem with Javascript in Master Pages- Any Ideas?

    02-08-2007, 5:53 PM
    • Loading...
    • jzamora
    • Joined on 02-01-2007, 2:55 PM
    • Costa Rica
    • Posts 1

    Hi, This jzamora!

    I did this example to show something, maybe this can solve someones problem:

    I saw one interesting thing on master pages.   you can call a .js file from your master page like this:

             <script language="javascript" type="text/javascript" src="Script/TestScript.js" ></script>    :: the script is in folder "Script"

    Then, i created a button and added the funtion event to it like this:

             <input id="Submit1" type="submit" value="Press the Button" onclick="testPopup()" />       :: testPopup() in in file "Script/TestScript.js"
     

    Case 1: if you create an aspx web page in the same directory as the master page, javascript works well

    Case 2: if the page that is using tha Master Page is in a sub directory like /OtherPages/MyPage.aspx, The inherited page is enogh capable to understand where is Master Page, BUT, right there, Master Page is not capable to resolve the javascript URL, so, Master Page thinks javascript file url is src="Script/TestScript.js"  and it should be src="../Script/TestScript.js".

    This create a big issue in managing javascript Url on subdirectories. I recommed to create varios Javscript files, with different urls in master page, so any page in subdirectories can call them.

    then, is good practice to create various .js. Ok, if U are using various, try to define specific functions and javascript programming for subdirectories only, or general files for all folder levels.

     

     

    Juan Zamora Mora
    System Engineer
    GPI Consultores, Microsoft Gold Certified Partner.
  • Re: Problem with Javascript in Master Pages- Any Ideas?

    02-08-2007, 11:39 PM
    • Loading...
    • terminator
    • Joined on 02-09-2007, 4:20 AM
    • Posts 13
    I have a different sort of problem. I have got an HTML web application where the top-menu is done using javascript. The .js files resides in a user-defined directory and they are called into the HTML files wherever needed.
     
    The top-menu is a .jpg file, and the dropdowns works using the javascript perfectly.
     
    I recently used the "Site Map" component of ASP.NET 2.0 to install "BreadCrumbs", and the BreadCrumbs are working perfectly without any problems. (I placed the whole HTML codes inside the "content control" tags,
     
    e.g.
    <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="speeches.aspx.vb" Inherits="newsroom_speeches_Default" %>

    <

    asp:Content ID="title" ContentPlaceHolderID="ContentPlaceHolder1" runat="server" >

    .............

    <

    script language="JavaScript" src="../../jsfiles/menus.js" type="text/JavaScript"></script>

    <

    script language="JavaScript" src="../../jsfiles/main.js" type="text/JavaScript"></script>

    ..............

    HTML CODES including the top-menu mapping coordinates

    </asp:Content>

    The problem is now is that the dropdowns are not rendered perfectly. I don't how I can describe this....its such that the "menu items" are compressed and the background color of that menu only exists behind the menu items (words) and not the whole menu. Let me know if anyone would like to see the menus to sort of the problem.
     
    But when I take out the "content" tags, it works perfectly.
     
    Have anyone experienced this problem before? Please do help me if anyone can sort of the actual problem
     
    Thanking you in advance.
  • Re: Problem with Javascript in Master Pages- Any Ideas?

    03-10-2007, 4:57 PM
    • Loading...
    • moshi
    • Joined on 10-07-2006, 7:15 PM
    • Posts 1

    Just for the record:

    I had the same symptom, the javascript function given for body onload just did not run. (It is the initialization of google maps.) It seemed that the onload event never occured. What makes it more interesting, it really worked for a long time but a few days ago I made changes to my application resulting in this problem. I searched the net and found this topic but the solution does not work for me, I do not use themes in my application so I do not have an App_Themes folder. 

    Now I have had an idea and just renamed my App_Browsers folder. Nothing else. And it works again!

    The logical conclusion is that if you install CSS Friendly Adapters to your ASP.NET 2.0 application, you should forget about the onload event of the body/window.
     

  • Re: Problem with Javascript in Master Pages- Any Ideas?

    06-13-2007, 10:07 AM
    • Loading...
    • ronney
    • Joined on 06-13-2007, 1:44 PM
    • Posts 1

    Hi,

    I've had the same problem in my recent project and, searching for a solution, I've found this page: http://www.odetocode.com/Articles/450.aspx
     

    The two basic ways to solve this problem are:

     
    1) Add a method in master page that include a script tag in header of page, like this:

      

    private void addScriptInc(string path)
    {
    HtmlGenericControl script = new HtmlGenericControl("script");
    script.Attributes.Add("type", "text/javascript");
    script.Attributes.Add("language", "javascript");
    script.Attributes.Add("src", ResolveClientUrl(path));
    Page.Header.Controls.Add(script);
    }

     
     or use the Page.ClientScript.RegisterClientScriptInclude method to add a script include inside body>form tags (i don't like this method)

     

    2) Write the script tag like this:

     

    <script src="<%= ResolveClientUrl("script_path/script.js") %>" type="text/javascript"></script>
      

    The two solutions above can be used with other elements, like links, images, etc.
     

    hope it'll be helpful for you

    ps.: I'm Brazilian, so my english isn't good, sorry!!!

  • Re: Problem with Javascript in Master Pages- Any Ideas?

    02-07-2008, 1:24 PM

    Thankyou Ronney !! Your solution is perfect as is your English Smile


    Bad Code
    File :: DefaultMaster.master

    <!--Root Level-->
    <script type="text/javascript" src="js/CommonJavascript.js"></script>
    <!--For One Level Deep pages-->
    <script type="text/javascript" src="../js/CommonJavascript.js"></script>
    <!--For Two Level Deep pages-->
    <script type="text/javascript" src="../../js/CommonJavascript.js"></script>


    Good Code
    File :: Components/Header.ascx  (.. just an example)

    <script src="<%= ResolveClientUrl("../js/CommonJavascript.js") %>" type="text/javascript"></script>

    Note : This code doesnt work from root level Master file

Page 2 of 2 (24 items) < Previous 1 2
Microsoft Communities
Page view counter