I am trying to run my JavaScript code in SharePoint2010 but I don't know how to do that.

I created a visual web part and added the following simple JavaScript code, but it's not executing in my SharePoint page when I add the web part to my page

<Script type="text/javascript">
$(document).ready(alert("JavaScript code"));

How can I add my javascript to the sharepoint page?

  • You can add javascript to SharePoint page using SharePoint designer. Have you tried this.? Commented Dec 2, 2013 at 12:03
  • no. I'll give it a try.
    – Anand
    Commented Dec 2, 2013 at 12:06
  • Ohk.. Try it first. I think it might solve your problem. Commented Dec 2, 2013 at 12:07
  • 3
    I think, you might have missed the reference to jQuery script file. Either add the latest jQuery script file in the master page or script tag just above you add $(document).ready(...) stuff with src.
    – suryakiran
    Commented Dec 2, 2013 at 13:04
  • see suryakiran comment, also use _spBodyOnLoadFunctionNames.
    – Alexander
    Commented Dec 2, 2013 at 13:53

4 Answers 4


Open developer panel in your browser and check that there are no errors. In your case it seems that you not add jquery reference to the page). About how you can add javascript to the sharepoint page:

  • Content Editor Web Part - Use a CEWP when you want to add code to a single web page that is also a web part page. (See the Web Part chapter of my book for ways to see if a page is a web part page)
    Features: Easy to use (Source Editor in 2007 and HTML editor in 2010) Easy to reuse (Exportable - see the Web Part Must Knows chapter of my book for details) Can be placed directly in the web part page that needs the JavaScript or CSS JavaScript or CSS can also be added by linking to a text file stored in a SharePoint library. This file can have any extension, but using .HTM will let you open the file directly into SharePoint Designer.
  • Directly in a page - Use SharePoint Designer when you want to directly edit a Basic Page, Web Part page or a site page. You can add the JavaScript inside of tags, the CSS inside of tags, or link to a file that contains the code. Typically add your code just before the ending content tag for PlaceHolderMain. ()
  • Master Page - Use SharePoint Designer to add code to a master page when you want to code to be available on every page in a site. You can add the JavaScript inside of tags or link to a file that contains the JavaScript. Typically add your CSS just before the section and your JavaScript just before the tag.

See the link


if you intend to use a CEWP, then link it to a .js file which contains the following code:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script language="javascript" type="text/javascript"> 
$(document).ready(function() {
alert("JavaScript code");

Store this .js file in a SharePoint library and then link it to the CEWP.


This may not be the actual root of you issue, but there's a known incompatibility issue with jQuery and one of the SharePoint Javascript.

Especially, the file cmssitemanager.js redefine the $ alias (see $ in cmssitemanager.js conflicts with $ in jQuery).

To solve the issue, I suggest you to refactor your jQuery code like this:

<Script type="text/javascript">
    (function($) {
         $(document).ready(alert("JavaScript code"));

this construction will avoid the conflict by creating a local $ variable which will be provisionned by the actual jQuery object, and not the sharepoint's one.


Few Issues:

First, You are not referencing JQuery. Add one of the references below to your script and it should work.

You can use web references like the ones below:

(you will need to close and correct the brackets as I have intentionally removed them to be visible)

script src="http://code.jquery.com/jquery-latest.js"

script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"

Second, $(document).ready would work if you add a $(document).Execute or $(document).Delay until the script is loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.