Skip to main content

Cloud

Referencing Javascript files in Web Parts

To reference Javascript files in web parts, I’ve seen people adding them as embedded resources to the web part assembly. Problem is, simple updates to the Javascript in those files requires redeployment of the DLL and an iisreset.

Another way of referencing JS files is doing the below in CreateChildControls method. This way you can make updates to JS file independent of the web part assembly.

Literal ltlJS = new Literal();
ltlJS.Text = "<script type="text/javascript" src="" + SPContext.Current.Web.Url + "/_layouts/inc/Test.Js"></script>";
this.Page.Header.Controls.Add(ltlJS);

.ExternalClass94D02F4A83B6493D81A1EF4081139C6A .csharpcode, .ExternalClass94D02F4A83B6493D81A1EF4081139C6A .csharpcode pre
{font-size:small;color:black;font-family:consolas, "Courier New", courier, monospace;background-color:#ffffff;}
.ExternalClass94D02F4A83B6493D81A1EF4081139C6A .csharpcode pre
{margin:0em;}
.ExternalClass94D02F4A83B6493D81A1EF4081139C6A .csharpcode .rem
{color:#008000;}
.ExternalClass94D02F4A83B6493D81A1EF4081139C6A .csharpcode .kwrd
{color:#0000ff;}
.ExternalClass94D02F4A83B6493D81A1EF4081139C6A .csharpcode .str
{color:#006080;}
.ExternalClass94D02F4A83B6493D81A1EF4081139C6A .csharpcode .op
{color:#0000c0;}
.ExternalClass94D02F4A83B6493D81A1EF4081139C6A .csharpcode .preproc
{color:#cc6633;}
.ExternalClass94D02F4A83B6493D81A1EF4081139C6A .csharpcode .asp
{background-color:#ffff00;}
.ExternalClass94D02F4A83B6493D81A1EF4081139C6A .csharpcode .html
{color:#800000;}
.ExternalClass94D02F4A83B6493D81A1EF4081139C6A .csharpcode .attr
{color:#ff0000;}
.ExternalClass94D02F4A83B6493D81A1EF4081139C6A .csharpcode .alt
{background-color:#f4f4f4;width:100%;margin:0em;}
.ExternalClass94D02F4A83B6493D81A1EF4081139C6A .csharpcode .lnum
{color:#606060;}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Raja Ayyapusetty

Raja is a software consultant with over 9 years of experience in designing, developing and testing complex solutions using Microsoft technologies. Throughout his career he has worked primarily on SharePoint and .NET technologies. Raja has worked with SharePoint since the 2003 version and has deep understanding of its features and capabilities. Raja is also a certified Sitecore developer.

More from this Author

Follow Us