locked
dynamically add javascript file into header RRS feed

  • Question

  • User1861430547 posted

    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?

    <link href='"../../css/clientview.css"' type='"text/css"' rel='"stylesheet"'>
    Sunday, May 11, 2008 5:47 PM

Answers

  • User1702256038 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 13, 2008 2:33 AM

All replies

  • User1001868398 posted

    Use instead RegisterStartUpScript to do that.

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

     

    Sunday, May 11, 2008 6:06 PM
  • User1861430547 posted

    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?

    Sunday, May 11, 2008 6:29 PM
  • User1702256038 posted

    Hi,

    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.
    Tuesday, May 13, 2008 12:41 AM
  • User1861430547 posted

    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?

    Tuesday, May 13, 2008 1:03 AM
  • User1702256038 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 13, 2008 2:33 AM
  • User1861430547 posted

    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?

    Wednesday, May 14, 2008 12:40 AM