DotNetNuke Powered!
          
John Mitchell's (mostly DotNetNuke) Blog - Add a Javascript link to the Head section from your skin
 Tuesday, May 23, 2006

Ever wanted to include some cool Javascript with your skin?

Here is a way to make it show up properly in the Head section of the page. 

Just paste this code directly into your skin.

<script runat="server">
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'add a script reference for Javascript to the head section
Dim oLink As New HtmlGenericControl("script")
oLink.Attributes("language") = "javascript"
oLink.Attributes("type") = "text/javascript"
oLink.Attributes("src") = SkinPath & "MyJavascript.js"
Dim oCSS As Control = Me.Page.FindControl("CSS")
If Not oCSS is Nothing Then
oCSS.Controls.AddAt(0, oLink)
End if

End Sub
</script>

Updated 12/27/2006:

I have had a few requests from people to know how to include more than one file.  To do that you just need to change the link object for each file, then add it to the Controls collection of the css placeholder like this:

<script runat="server">
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
Dim oCSS As Control = Me.Page.FindControl("CSS")
 If Not oCSS is Nothing Then

   'add a script reference for Javascript to the head section
   Dim oLink As New HtmlGenericControl("script")
   oLink.Attributes("language") = "javascript"
   oLink.Attributes("type") = "text/javascript"
   oLink.Attributes("src") = SkinPath & "MyJavascript.js"
   oCSS.Controls.AddAt(0, oLink)

   'add another link reference
   oLink.Attributes("src") = SkinPath & "YourJavascript.js"
   oCSS.Controls.AddAt(0, oLink)

   'and to add a different kind of link, like to a CSS file you should clear the old attributes of the link object by creating a new one

  oLink = New HtmlGenericControl("link")
  oLink.Attributes("rel") = "stylesheet"
  oLink.Attributes("type") = "text/css"
  oLink.Attributes("href") = SkinPath & "MyStyleSheet.css"
  oCSS.Controls.AddAt(0, oLink)

  End if

End Sub
</script>


5/23/2006 1:19:14 PM (Central Daylight Time, UTC-05:00)  #    Comments [11]