Setting up Relative path for script tag

the tags with out runat attribute will not allow us to set relative path like

“~/script/xyz.js”

to over come this issue you have to create the tag at runtime in the page load event like this.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
         Dim js As HtmlGenericControl = New HtmlGenericControl("script")
       js.Attributes.Add("type", "text/javascript")
       Dim s As String = "~/js/jquery.js"
       s = s.Replace("~", Request.ApplicationPath)
       js.Attributes.Add("src", s)
       Page.Header.Controls.Add(js)
   End Sub

again you will be facing one problem is that the script tag generated will be added at the end of the header if you have a function which is going to use the referenced script library will give you javascript error.

once again a not so good solution is put your script inside the body of the page . now everything will works fine.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.