脚本似乎在jQuery加载之前运行 - 未捕获的ReferenceError:$未定义

时间:2013-12-09 21:58:14

标签: javascript jquery sharepoint asynchronous labjs

我正在编辑一个sharepoint母版页,其中包含我头部标记的顶部,调用labJS和scriptloader.js文件。

这是该文件包含的内容:

$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")

在我的sharepoint网站上,我有一个较小的html页面,其中包含一个时钟

<!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>

我的控制台在我的页面的第1472行给出了这个错误,但是在开始标记之后基本上就调用了scriptloader.js文件。

2 个答案:

答案 0 :(得分:1)

尝试使用_ spBodyOnLoadFunctionNames数组而不是document.ready。

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

答案 1 :(得分:1)

当我在SP上遇到JQuery问题时,我使用了这个片段:

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

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