I'm not ready to package it up into an extension yet, but here it is. It's usable from ASCX skin files only. I was frustrated with the inability to add script to page headers from my skin. So this script plugs into the ASP.NET ScriptManager to add script includes, or script blocks to the page header. TESTED IN DNN 5.2.0 ONLY! And I shouldn't have to mention that you should use it at your own risk.
Syntax:
All Posible Options:
<uc1:script runat="server"
id="something"
Src="string"
RequireJquery="false"
UseScriptManager="true"
AddScriptTags="false"
IsStartupScript="false"
>
//javascript literal
</uc1:script>
where:
id The key of the script tag. Useful to prevent naming
collisions between controls, modules, and script.
e.g. Your skin file and containers can all include
jQueryUI, but the ScriptManager automatically includes
the necessary js file ONCE per unique id value.
RequireJquery Triggers DNN jQuery to be loaded. (default=false)
UseScriptManager
true = Uses System.Web.UI.ScriptManager to manage the script inclusion.
false = includes the script files manually. (NOT IMPLEMENTED)
(default=true)
AddScriptTags Use with the script literal (tag inner contents). (default=false)
IsStartupScript Delay script execution until the page is fully loaded. (default=false)
Usage:
<%@ Register TagPrefix="uc1" TagName="SCRIPT" Src="~/path/to/script.ascx" %>
<uc1:script id="test1" src="js/test.js" runat="server"></uc1:script>
<uc1:script id="test2" runat="server" IsStartupScript="true" AddScriptTags="true">
this is test2 script!");
</uc1:script>
<uc1:script id="test3" src="js/lightbox.js" runat="server" RequireJquery="true"></uc1:script>
Source: script.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="script.ascx.cs" Inherits="skin_script" %>
Source: script.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
[DefaultProperty("Src"),
ParseChildren(true, "ScriptLiteral" ),
ToolboxData("<{0}:Script runat=server>")]
public partial class skin_script : DotNetNuke.UI.Skins.SkinObjectBase {
public db4o_script()
: base() {
this.Src = null;
this.ScriptLiteral = null;
this.RequireJquery = false;
this.UseScriptManager = true;
this.AddScriptTags = false;
this.IsStartupScript = false;
}
#region Properties
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
DefaultValue(null)]
public string Src { get; set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
DefaultValue(null),
PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public string ScriptLiteral { get; set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
DefaultValue(false)]
public bool RequireJquery { get; set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
DefaultValue(true)]
public bool UseScriptManager { get; set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
DefaultValue(false)]
public bool AddScriptTags { get; set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
DefaultValue(false)]
public bool IsStartupScript { get; set; }
#endregion
protected void Page_Load(object sender, EventArgs e) {
if (!String.IsNullOrEmpty(this.Src) && !String.IsNullOrEmpty(this.ScriptLiteral))
throw new ArgumentException("Cannot specify both a src attribute and a script body.");
if (!this.UseScriptManager)
throw new NotImplementedException();
string id = this.ID.ToLowerInvariant();
if (this.RequireJquery) {
if (!DotNetNuke.Framework.jQuery.IsInstalled)
throw new NotSupportedException("jQuery is required, but not installed.");
DotNetNuke.Framework.jQuery.RequestRegistration();
}
if (!String.IsNullOrEmpty(this.Src))
AddScriptSrcToPage(id, this.Parent.ResolveUrl(this.Src));
if (!String.IsNullOrEmpty(this.ScriptLiteral))
AddScriptToPage(id, this.ScriptLiteral);
}
protected void AddScriptToPage(string key, string scriptBody) {
if (UseScriptManager) {
if (DotNetNuke.Framework.AJAX.IsInstalled())
DotNetNuke.Framework.AJAX.RegisterScriptManager();
if (!IsStartupScript)
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), key, scriptBody, this.AddScriptTags);
else
ScriptManager.RegisterStartupScript(this, this.GetType(), key, scriptBody, this.AddScriptTags);
}
else {
}
}
protected void AddScriptSrcToPage(string key, string url) {
if (UseScriptManager) {
if (DotNetNuke.Framework.AJAX.IsInstalled())
DotNetNuke.Framework.AJAX.RegisterScriptManager();
ScriptManager.RegisterClientScriptInclude(this, this.GetType(), key, url);
}
else {
}
}
}