Adding jQuery to a SharePoint Page: Step One, Always

No matter how many times I add jQuery to a page in SharePoint, this is absolutely *always* the first thing that I do:

<script language="javascript" type="text/javascript" src="/Scripts/jquery-1.5.min.js"></script>
<script language="javascript" type="text/javascript" src="/Scripts/jquery.SPServices-0.6.1.min.js"></script>
<script language="javascript" type="text/javascript">

	$(document).ready(function() {

		alert("jQuery");

	});

</script>

SNAGHTML168ed5faIf that alert in line 7 which is wrapped in a $(document).ready fires, then my script references are good. I can’t tell you how many times I’ve talked people through that simple step. It even hangs me up, so it’s always good to do that alert first. (Sure, the alert can say “Bob’s your Uncle” or “Strawberry Jam”, but alert *something*.)

51 Comments

  1. How about alert(“jQuery:”+window.jQuery); ? I mean, sometimes you need jQuery before the document is ready.
    Also, what do you mean by “No matter how many times I add jQuery to a page”? Once should be enough, right? ;-)

    Reply
  2. Help! The pop-up does not appear for me, leading me to the obvious conclusion – I am doing something wrong. Where can I start troubleshooting why the calls to the .js files are not working? The paths are correct (they work via a browser) and the permissions seems to be correct as well.

    Any advice?

    Reply
    • Andrew:

      If you’re not getting the popup, then the references to the .js files aren’t right. If you open the page in SharePoint Designer, you can Ctrl-Click on the src links to see if they are valid.

      M.

      Reply
  3. Hi, I am adding the script to a customized NewForm.aspx. i have added the v0.7.0 inside PlaceHolderMain. From the SPDesigner i can navigate to the .js references but still no pop-up. i am new to jquery. please assist. thanks.

    Reply
  4. hey, i am using exactly what you say but still my alert is not working.. this is the script i am using in the content editor __ source editor, please can you help me:

    $(document).ready(function() {
    alert(“Hello world!”);
    });

    Hi

    Reply
      • Hi thank you for your reply, no i am not. but i have tried out something else in the meantime: i have pasted the whole jQuery build in my source editor and then it did work.

        Reply
        • That’s not a great idea from a maintainability perspective. you should store the .js file somewhere accessible by HTTP, like a Document Library, the _layouts folder on the WFEs, or by referencing an external CDN.

          M.

          Reply
          • well i tried to reference an externam cdn but thats what didnt work because the site is secured by vpn, which is i think also why when i add it to a library it also didnt work. when i refered to tht library it wud give page not found.

            Reply
  5. Marc – I am having problems getting the alert to fire.
    Steps taken:
    Created new doc library named scripts
    Added both .js files to the new document library called Scripts
    Created http://sitename/qtest.aspx via sharepoint designer
    Tested accessing the new page successfully via browser
    I also tried placing the .js files in _layouts and referenced this path in the src but same behavior occurred – page opened fine without errors just did not fire the jquery alert.
    —-
    qtest.aspx page output is as follows:

    $(document).ready(function() {
    alert(“hello jQuery”);
    });

    test 1

    This is a test

    I am not seeing any errors when accessing the page. It returns “This is a test” with no problems. It just does not fire the jquery alert. Tried this on IE9.x firefox 11.x and IE8.x

    Any ideas?

    Reply
      • Thank you for your response. Yes the js references are entered as follows:(placed in brackets/quotes hopefully this time they are accepted:)
        [“…

        $(document).ready(function() {
        alert(“hello jQuery”);
        });…”]

        Reply
        • kc:

          That means that your reference to the jQuery file isn’t valid. If you have the page open in SharePoint Designer, you can Ctrl-Click the src. If it is valid, the file will open for editing; if not, you’ll get a message saying so.

          M.

          Reply
  6. Hey Marc,

    having jquery links markup in my page or in CEWP works great on my local environment. Jquery files are in one of the document library. But not able to understand when I put this on client server page loads a while and then gives 404 (wierd Isnt it ??)

    Can you please assist ? Is it because of some configuration ? this happens for CSS links too :(

    Reply
    • Swapnil:

      I’m not sure I follow what’s going on, but take a look at the Net traffic with Fiddler or Firebug to see if there are issues with the file references or authorization.

      M.

      Reply
  7. I have a list that has three columns [Cust] [Site] [Line] each are of type lookup. What is the simplest way to get a combination of the three ie “MyCustomer MySite MyLine” back into a column as a single line of text in the same list.
    Any help appreciated!!
    Thanks

    Reply
    • Drew:

      You wont be able to use the Lookup columns in a formula, so you’ll can resort to a little workflow to populate the fourth column. In the workflow, you can concatenate the three values.

      I would probably do it with script in the forms, though. In the PreSaveAction(), I’d populate the fourth column based on the contents of the first three before saving the form.

      M.

      Reply

Have a thought or opinion?