Script seems to be running before jQuery loads - Uncaught ReferenceError: $ is not defined

1.3k views Asked by At

I'm editing a sharepoint master page which contains at the very top of my head tag, a call to labJS and a scriptloader.js file.

This is what this file contains:

$LAB
    .script("/Style Library/libs/jquery-1.10.2.min.js")
    .script("/Style Library/libs/jquery-ui.min.js")
    .script("https://www.google.com/jsapi")
    .script("/Style Library/libs/jquery.SPServices-2013.01.min.js")
    .script("/Style Library/libs/angular.min.js")
    .script("/Style Library/libs/knockout-3.0.0.js")
    .script("/Style Library/addons/wpToggle/wpToggle-jQuery.js")
    .script("/Style Library/addons/spCharts/spjs-charts-v4.js")
    .script("/Style Library/addons/quickLaunchToggle/jQuery.LISP.quicklaunch.js")

On my sharepoint site I have a smaller html page which contains a clock

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>CSS3 Digital Clock with jQuery</title>
<style type="text/css"></style>
<script type="text/javascript">
$(document).ready(function() {
// Create two variable with the names of the months and days in an array
var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; 
var dayNames= ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]

// Create a newDate() object
var newDate = new Date();
// Extract the current date from Date object
newDate.setDate(newDate.getDate());
// Output the day, date, month and year    
$('#Date').html(dayNames[newDate.getDay()] + ", " +monthNames[newDate.getMonth()]+ ' ' +  newDate.getDate()  + ', ' + newDate.getFullYear());

setInterval( function() {
    // Create a newDate() object and extract the seconds of the current time on the visitor's
    var seconds = new Date().getSeconds();
    // Add a leading zero to seconds value
    $("#sec").html(( seconds < 10 ? "0" : "" ) + seconds);
    },1000);

setInterval( function() {
    // Create a newDate() object and extract the minutes of the current time on the visitor's
    var minutes = new Date().getMinutes();
    // Add a leading zero to the minutes value
    $("#min").html(( minutes < 10 ? "0" : "" ) + minutes);
    },1000);

setInterval( function() {
    // Create a newDate() object and extract the hours of the current time on the visitor's
    var hours = new Date().getHours();
    // Add a leading zero to the hours value
    $("#hours").html(( hours < 10 ? "0" : "" ) + hours);
    }, 1000);

}); 
</script>
</head>
<body>
<div class="clockContainer">
<div class="clock">
    <div id="Date"></div>
        <ul>
            <li id="hours"> </li>
            <li id="point">:</li>
            <li id="min"> </li>
            <li id="point">:</li>
            <li id="sec"> </li>
        </ul>
    </div>
</div>
</body>
</html>

My console gives me this error at line: 1472 of my page but the scriptloader.js file is called basically right after the opening tag.

2

There are 2 answers

0
Yevgeniy.Chernobrivets On BEST ANSWER

Try to use _spBodyOnLoadFunctionNames array instead of document.ready.

<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("myCustomFunctionName");
function myCustomFunctionName() {
...
} 
</script>
1
langus On

When I have problems with JQuery on SP, I use this snippet:

var j = jQuery.noConflict();
ExecuteOrDelayUntilScriptLoaded(run, "sp.js");

function run(){
   j('#selector').method();
}