Friday, September 19, 2008

Using JavaScript in Content Editor webpart

Adding the script in a ContentEditor webpart is very staraightforward and easy too. For example if we want to execute a script while loading the page, we just need to add a content editor webpart in the page and add the script in the Source editor of the content editor webpart. For example to give a simple alert message while loading the page, the follwoing steps are suffient.

1.Open the page in edit mode.

2.Add a content editor webpart and click on "Modify shared webpart" option.

3.Click on the sourceEditor button and paste the following simple code(with correct script tag) in the editor window. Click on Save button
<_script type="text/javascript" language="javascript">
var url;
url = window.location.toString()
alert("Location: "+ url);


4.Check the "Hidden" check box under the "Layout" section.[This will not show the contenteditor webpart in the page, because we using it just for javascript sake]

5.Click on the "Ok" button and press exit edit mode link.

6.This should display the popup box mentioning the URL of the page.

No comments: