Thursday, July 16, 2009

Make sure your Javascripts are in the proper location in DotNetNuke

Calling the procedure below in your DotNetNuke module will make sure that your client script will be inserted in the proper place. Which is inside the Head tag.

The Code:
01.Public Sub InjectClientScript(ByVal page As System.Web.UI.Page, ByVal scriptKey As String, ByVal scriptSrc As String, Optional ByVal scriptValue As String = "")
02.    Dim objHead As Control = page.FindControl("Head")
03.    If objHead IsNot Nothing Then
04.        If objHead.FindControl(scriptKey) Is Nothing Then
05.            Dim jQueryControl As New HtmlGenericControl("script")
06.            jQueryControl.ID = scriptKey
07.            jQueryControl.Attributes.Add("type", "text/javascript")
08.            If scriptValue = "" Then
09.                jQueryControl.Attributes.Add("src", scriptSrc)
10.            Else
11.                jQueryControl.InnerHtml = scriptValue
12.            End If
13.            objHead.Controls.Add(jQueryControl)
14.        End If
15.    End If
16.End Sub



Sample:
Inserting a script file;
1.InjectClientScript(page, "jquery.1.3.2.min.js", "/js/jquery.1.3.2.min.js")

Result:
1.<script id="jquery.1.3.2.min.js" type="text/javascript" src="/js/jquery.1.3.2.min.js"></script>


Inserting a plain script;
1.InjectClientScript(page, "jQueryNoConflict", "", "jQuery.noConflict();")

Result:
1.<script id="jQueryNoConflict" type="text/javascript">jQuery.noConflict();</script>





No comments:

Post a Comment