dynamically add javascript file into header

Last post 05-14-2008 12:40 AM by jigsy. 6 replies.

Sort Posts:

  • dynamically add javascript file into header

    05-11-2008, 5:47 PM
    • Member
      1 point Member
    • jigsy
    • Member since 05-21-2007, 3:34 AM
    • Posts 16

    I am trying to dynamically add a javascript file to the header tag so that I can access a javascript library that I have created.  I have tried and tried to get this code to work but to no avail.  I am a rank amatuer and I think I am missing something simple.  Any suggestions would be greatly appreciated.

    The error:
     

    Cannot use a leading .. to exit above the top directory.

    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    public class index : Page
    {

     protected void Page_Load(object sender, EventArgs e)
     { 
      
      HtmlGenericControl js = new HtmlGenericControl("script");
      js.Attributes.Add("type", "text/javascript");
      js.Attributes.Add("src", "http://www.blushifted.com/javalibrary.js");
      this.Page.Header.Controls.Add(js);  
     
     }
     
    }

    my masterpage looks like this:

    <Head runtat="server">

    </Head>

    has anyone been able to get this to work?

  • Re: dynamically add javascript file into header

    05-11-2008, 6:06 PM
    • All-Star
      17,458 point All-Star
    • albertpascual
    • Member since 05-23-2003, 2:11 PM
    • Riverside, CA
    • Posts 3,474

    Use instead RegisterStartUpScript to do that.

    http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx

     

    Cheers
    Al
    My Blog
    MapStats.NET
    Please click on 'Mark as Answer' if this post answered your question!
  • Re: dynamically add javascript file into header

    05-11-2008, 6:29 PM
    • Member
      1 point Member
    • jigsy
    • Member since 05-21-2007, 3:34 AM
    • Posts 16

    I am sorry but I am new to web programming.  Where would I put the registerscript code?  Could you show me a simple example using an external .js file?

  • Re: dynamically add javascript file into header

    05-13-2008, 12:41 AM

    Hi,

    jigsy:
    Where would I put the registerscript code?  Could you show me a simple example using an external .js file?

    You can try to put the registerscript code in the page's load event, for example:

    There is a JavaScript file called myJavaScript.js, and you would like include it inside the web form by using the RegisterClientScriptInclude method, as shown in the following:

    ClientScript.RegisterClientScriptInclude("MyScript","MyJavaScript.js")

    This piece of code will inject the following JavaScript include in your html code generated by the server:

    <script src="MyJavaScript.js" type="text/javascript"></script>
    Hope it helps.
    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: dynamically add javascript file into header

    05-13-2008, 1:03 AM
    • Member
      1 point Member
    • jigsy
    • Member since 05-21-2007, 3:34 AM
    • Posts 16

    Yep, I got that to work but when I tried to use one of the functions in the file I got an error.  Let me ask you this if you will, how would i reference the functions in the file?  Do I have to do anything more than reference the function directly?  For instance, if I had a display fuction that I wanted to use on a button, would I just reference that function directly?

  • Re: dynamically add javascript file into header

    05-13-2008, 2:33 AM
    Answer

    Hi,

    For example there is a js file named: JScript.js,

    there is only a function in the JScript.js file, like below:
    function showWelcome()
    {
        alert("Hello World!");
    }

    then register this script file like this in the page's load event:

      protected void Page_Load(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptInclude("script", "JScript.js");
        }

    then you can use the javascript function in the page's aspx file:

        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" OnClientClick="showWelcome()"/>

    Hope  it helps.

    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: dynamically add javascript file into header

    05-14-2008, 12:40 AM
    • Member
      1 point Member
    • jigsy
    • Member since 05-21-2007, 3:34 AM
    • Posts 16

    Thank you soooo much for that, its what I needed.  I could not find anything like that via google.  After you run that code, how would you pass that on to a server-side function?  So if you wanted to run a js function first and then if it passes some qualification you can run a server side function?

Page 1 of 1 (7 items)