Register a javascript file (.js) in C# ASP.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • querry
    New Member
    • Feb 2007
    • 45

    Register a javascript file (.js) in C# ASP.NET

    Hi all,

    I have a little problem, I want to register a javascript in a web page. I know it is done with the help of "Page.ClientScr ipt.RegisterSta rtupScript()" method.

    But this methods accepts the javascript as a string as one of its parameters.

    My problem is to register the javascript file that is the .js file, which contains the javascript functions.

    Is there a way to do so??

    Many Thanks.
  • wgale025
    New Member
    • Feb 2007
    • 23

    #2
    Response.write( "<script type=\"text/javascript\" defer>"+javascr ipt function here+"</script>");

    Comment

    • Antek71
      New Member
      • Sep 2008
      • 2

      #3
      Hi Querry,

      I know your original post is quite old now, but maybe this might help someone else. Try the following:

      Code:
      if( !Page.ClientScript.IsClientScriptIncludeRegistered)   {
          Page.ClientScript.RegisterClientScriptInclude("myFile", "myJSFile.js");
         }
      This works with Dot Net 2.0 and above and renders the following in the <Head> tag of the page:

      Code:
      <script src="myJSFile" type="text/javascript"></script>

      The only downside to the above is that the <script> tag is added just after the VIEWSTATE within the <body> tag. I imagine you could probably override the 'RegisterClient ScriptInclude' method somehow to change this behaviour, but I've not looked into this.



      Cheers,
      Anthony.

      Comment

      • Antek71
        New Member
        • Sep 2008
        • 2

        #4
        Sorry! My bad... I meant <BODY> not <HEAD> in the above post!

        Should have been...
        "This works with Dot Net 2.0 and above and renders the following within the <BODY> tag of the page..."


        Cheers,
        Anthony.

        Comment

        Working...