Adding jQuery+SPServices to a SharePoint Page: Step One, Always

alert(

As a follow up to my prior post Adding jQuery to a SharePoint Page: Step One, Always, here’s another step one, always, for when you are using jQuery+SPServices. Even if you have referenced the jQuery library correctly, you still may not be referencing SPServices correctly.

To make sure that SPServices is referenced correctly, add this line:

  alert($().SPServices.SPGetCurrentSite());

right after

$(document).ready(function() {
  alert("jQuery");

Alert GetCurrentSite()The first alert will ensure that your jQuery reference is correct. The second will ensure that your SPServices reference is correct, assuming that you see the name of your current site. If you see “undefined” or some value which makes no sense, odds are your reference to SPServices is wrong.

If you get both alerts, you’re past some large percentage of the issues I help with in the SPServices Discussions. The rule of thumb is relatively simple: scripts which depend on other scripts must be referenced after those dependencies. SPServices requires jQuery, so you reference jQuery first and then SPservices. If your dependencies are more complex, consider using a more sophisticated script loader, like the great LABjs from Kyle Simpson (@getify). You can also, of course, use SharePoint’s script loading logic, though I’ve never found that it offers much benefit for the complexity it adds.

Here’s the full simple step one test. Obviously, you’d replace the references with your own paths and filenames.

<script type="text/javascript" language="javascript" src="my_path/jquery-1.6.1.min.js"></script><script type="text/javascript" language="javascript" src="my_path/jquery.SPServices-0.6.2.min.js"></script>
<script type="text/javascript" language="javascript">
  $(document).ready(function() {
    alert("jQuery");
   alert($().SPServices.SPGetCurrentSite());
  });
</script>

Similar Posts

46 Comments

  1. Hi Marc,

    thanks for your quick reply. I did not stop working and just found the solution: I had to change the IE-Version in v4.master File from 8 to 9 or 10. edge did not work. :

    Greetings,

    Simon.

    1. @Simon:

      Edge is simply not ready for prime time yet. I’d avoid using it in the near term. They say it’s the best. but right now it isn’t.

      Also, if you mean changing the DOCTYPE, that’s a bad idea, as it can cause things in SharePoint to break.

      M.

    1. @Simon:

      You’ll find that changing the doctype will have side effects you don’t want. You should be able to add script references into the page without a problem, so I’d look at fixing whatever issue you have rather than changing the doctype.

      M.

  2. Yes! Dude I owe you a beer.
    as soon as I moved my j query.js before spservice.js it worked.
    very useful Blog

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.