Thursday, April 14, 2011

How to call external script (js) files in web part in SharePoint 2010?


This stuff is done by my Tech Lead K, Kathiravan from HP. He was working on a web part (not visual web part) and need to call an external .js file. Here is the code sample:

protected override void OnPreRender(EventArgs e)
{
Page.ClientScript.RegisterStartupScript(GetType(), "MyScript",
"<script language='javascript' src='/_layouts/1033/test.js'></SCRIPT>", false);
base.OnPreRender(e);
}

Put the js file as per the location given in the code. And one important thing to remember is that You should not write any script tag inside the js file, else it will give an error. Because script inside script is not allowed. You can also check SharePoint 2010 articles.

No comments:

Post a Comment