0

I have an old ASP.NET 1.1 site that I am maintaining. We are working with Google to place analytics code on all pages. Since I can't take advantage of master pages in 1.1, I have my pages include headers/footers/sidebars with User Controls.

What came to mind first is to place the JavaScript in my footer ascx control so it appears on every page. But I don't think I can link to a JavaScript file from a user control.

Any ideas on what I can do to get this js code placed on every page in my site?

2 Answers 2

5

What keeps you from simply referencing your script in the user control?

<asp:SomeControl ID="SomeControl1" runat="server>
  <script src="some.js" type="text/javascript"></script>
</asp:SomeControl>

You could also do this:

protected void Page_Load(object sender, EventArgs e)
{
  Literal some_js = new Literal();
  some_js = "<script type='text/javascript' src='some.js'></script>";
  this.Header.Controls.Add(some_js);
}

(Obviously, the second approach would still force you to modify the pages themselves, unless they inherit from a common parent you control.)

Sign up to request clarification or add additional context in comments.

1 Comment

Apprach 1 should work just fine - just put the analytics code into the HTML portion of the user control.
1

Create a base page class and load the script in base page. Further inherit all pages from base page.

Other way could be same as that suggested by Tomalak

HtmlGenericControl jscriptFile = new HtmlGenericControl();
jscriptFile.TagName = "script";
jscriptFile.Attributes.Add("type", "text/javascript");
jscriptFile.Attributes.Add("language", "javascript"); 
jscriptFile.Attributes.Add("src", ResolveUrl("myscriptFile.js"));
this.Page.Header.Controls.Add(myJs);

1 Comment

The solution for me was to create a base page and register the javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.