Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 5461 |  Welcome, Guest!   Register  Login
 Home > Forums > ASP.NET > Add script on every page ...
Ajayk3

Add script on every page

Replies: 3 | Posted by: Ajayk3 on 4/23/2012 | Category: ASP.NET Forums | Views: 333 | Status: [Member] | Points: 10  


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


Reply | Reply with attachment | Alert Moderator

 Responses below this adGet hundreds of .NET Tips and Tricks videos

 Replies

Swapnil
Swapnil  
Posted on: 4/23/2012 7:13:32 AM
Level: Starter | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

Ajayk3
Ajayk3  
Posted on: 4/23/2012 7:17:55 AM
Level: Starter | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

Swapnil
Swapnil  
Posted on: 4/23/2012 8:34:48 AM
Level: Starter | Status: [Member] | Points: 50

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. | Reply | Alert Moderator 

Reply - Please login to reply


Click here to login & reply

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/18/2013 6:50:56 PM