Please tell me where can I write javascript for a sharepoint site and what are the possible ways to write a javascript code for a sharepoint site.
4 Answers
Approach 1 :
You can define your java script code in code behind file and than you can register it using Page.IsClientScriptBlockRegistered as shown below.
private const string strJQueryScript = "<script type=\"text/javascript\">"+
Your Script content here.
"</script>";
Once you created your script you can later use following code in "OnPreRender" method.
protected override void OnPreRender(EventArgs e)
{
if (!Page.IsClientScriptBlockRegistered("jquerythickbox"))
Page.RegisterClientScriptBlock("jquerythickbox",
strJQueryScript);
}
Approach2:
You can place ContentEditorWebPart on the page and write Javascript code there.
Approach3:
Create your javascript file and put it in _Layouts folder. Once you have your javascript file you can modify your master page and register tag as shown below.
<script type="text/javascript" language="javascript" src="/_layouts/YourFolderName/YourFile.js"></script>
It can be even easier than the other answers: You can simply put the JavaScript into a Content Editor Web Part. It all depends on what your goals are, e.g., how widely you want to use the script, what it does, etc.
If you just want to add some JavaScript to the existing user interface without having to modify your existing pages or solution then check out our free Muhimbi SharePoint Infuser tool.
-
Could you please edit this answer to make it clear this is your company's product? It does answer the question directly so I guess isn't technically spam, but clear attribution would help. Thanks!Alex Angas– Alex Angas2011-02-15 02:33:58 +00:00Commented Feb 15, 2011 at 2:33
-
No worries Alex, I have reworded it. It is a free tool so didn't bother with the attribution.Jeroen Ritmeijer– Jeroen Ritmeijer2011-02-15 08:58:19 +00:00Commented Feb 15, 2011 at 8:58
Here is a question that I asked on the matter Click Here
As you will see I also made use of a CEWP.......