Add script on every page [Resolved]

Posted by Ajayk3 under ASP.NET on 4/23/2012 | Points: 10 | Views : 1525 | Status : [Member] | Replies : 3
Hi Friends,

I have one java script which i need to insert into every page.

I have many master pages so i don't want to add in every master pages.

My requirement is to add this script just before </body> tag.

I think to do this there may be 2 solutions:-

1) To add this in HTTPModule class

2) To overwrite prerender event of Page class.

But i want the exact code to do this.

If Any body has used this. Kindly help me .

Many Thanks in Advance

Ajay Kumar




Responses

Posted by: Swapnil on: 4/23/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
Yes Ajay it is possible. Above code will insert it in head tag. However if you want to put it before body tag. than following code snippet will work for you.

     string jsScript = "<script type=\"text/javascript\" language=\"Javascript\" src=\"yourscriptFile.js\">";

jsScript += "</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "Key", myScript, true);


Thanks and Regards,
Swapnil

Ajayk3, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Swapnil on: 4/23/2012 [Member] Starter | Points: 25

Up
0
Down
Please find code for 2nd option to override page class event and add following code.

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



Thanks and Regards,
Swapnil

Ajayk3, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ajayk3 on: 4/23/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Swapnil,

Thanks for the reply but i want to add this script just before the closing of body tag.

Is it possible?



Ajayk3, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response