Strange behavior when testing script references

Jul 17, 2013 at 8:03 PM
Hi, following the guide at http://sympmarc.com/2011/07/08/adding-jqueryspservices-to-a-sharepoint-page-step-one-always/, I've added the below code to my page. Ctrl+Clicking the script references works fine. When I try it on the page, the first SPGetCurrentSite() alert works fine. The second SPGetCurrentSite() alert inside the document.ready function gives the following error:

"Unable to get value of the property 'SPGetCurrentSite': object is null or undefined"

Why is it working outside of the document.ready function, but not inside?
<script type="text/javascript" src="/JQuery/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="/JQuery/jquery.SPServices-0.7.2.min.js"></script>

<script type="text/javascript">
alert($().SPServices.SPGetCurrentSite());

$(document).ready(function() {
alert("jQuery");
alert($().SPServices.SPGetCurrentSite());
  });
</script>
Coordinator
Jul 17, 2013 at 8:24 PM
Does the "jQuery" alert fire?

M.
Jul 18, 2013 at 1:32 AM
Yes, it does.
Coordinator
Jul 18, 2013 at 3:11 AM
I just added your script to a page and all three alerts fire for me. I'm not sure what's happening on your end.

M.
Jul 18, 2013 at 6:35 AM
Just as a thought - have you tried using jQuery 1.8.x instead, as a test? I appreciate you may need to use 1.72 for other things - it might at least help narrow things further...
Jul 18, 2013 at 1:52 PM
Ok, so I'm using jquery 1.8.3 and SPServices 2013 now. I placed both references on the masterpage instead of on the page I am working with. I'm getting a new error associated with the first line of the script ($(document).ready). Neither alerts are working now.

SCRIPT5007: The value of the property '$' is null or undefined, not a Function object
<script type="text/javascript">
$(document).ready(function() {
alert("jQuery");
alert($().SPServices.SPGetCurrentSite());

   });

Coordinator
Jul 18, 2013 at 1:56 PM
That's telling you that $ isn't defined, thus jQuery isn't defined.

It doesn't matter where you put the references, as long as the .js files are loaded before you attempt to use the functions that they provide.

These days, it's usually easier to load jQuery in the master since we all use it ubiquitously.

M.
Jul 18, 2013 at 1:59 PM
Edited Jul 18, 2013 at 2:10 PM
I think I need more coffee because I plopped the references in the wrong place on the MP. Whoops! So now I'm back to the Jquery alert working but the GetCurrentSite not working with error:

SCRIPT5007: Unable to get value of the property 'SPGetCurrentSite': object is null or undefined

Using Chrome dev tools, I see both JS files are loaded.
Jul 18, 2013 at 2:14 PM
Hah, so I fixed it. There was a rogue CEWP on the page referencing 1.8.2 jquery file. I removed the reference and it is alerting properly. :) Now time to try GetListItems!