In this article we will discuss about how to insert JavaScript code in
Content Editor WebPart in SharePoint. Also you can check out my previous posts on:
Here we will see how we can insert JavaScript into a Content Editor Web Part.
First open a notepad and write the JavaScript code. Here I have used simple code to write Hello World as below.
<script language="JavaScript">
document.write('Hello World !!!');
</script>
Now Save the file with a name(Here name is MyJavaScript.txt). Now we can put the text file in the Site Assets Library and also we can put the code in the layouts folder.
Copy the text file and put it inside the C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\STYLES folder.
Open the page where you want to add Content Editor Web Part and edit the page. After that in the edit mode click on Add web part and from the Ribbon select the Media and Content web part category and select Content Editor web part and then click on Add.
Now go to the Content Editor web part properties. From the properties select Content Link and give path of the text file that content the JavaScript code. Since we have saved the file in the layouts folder the path will look like:
http://[Server Name]/_layouts/Styles/MyJavaScript.txt .
Click on OK.
Then Save and Close the page, now our JavaScript code will appear.