Ama*_*son 101 javascript jquery
我遇到的问题是页面加载速度如此之快,以至于jquery在后续脚本调用之前尚未完成加载.有没有办法检查jquery的存在,如果它不存在,请稍等片刻,然后再试一次?
在回答下面的答案/评论时,我发布了一些标记.
情况...... asp.net主页和子页面.
在母版页中,我引用了jquery.然后在内容页面中,我引用了特定于页面的脚本.当加载特定于页面的脚本时,它会抱怨"$ is undefined".
我在标记的几个点放置警报,以查看触发事件的顺序,并确认它按此顺序触发:
这是主页顶部的标记:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="SiteMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Reporting Portal</title>
<link href="~/Styles/site.css" rel="stylesheet" type="text/css" />
<link href="~/Styles/red/red.css" rel="stylesheet" type="text/css" />
<script type="text/Scripts" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/Scripts" language="javascript" src="../Scripts/jquery.dropdownPlain.js"></script>
<script type="text/Scripts" language="javascript" src="../Scripts/facebox.js"></script>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
Run Code Online (Sandbox Code Playgroud)
然后在母版页的正文中,还有一个额外的ContentPlaceHolder:
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
Run Code Online (Sandbox Code Playgroud)
在子页面中,它看起来像这样:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Dashboard.aspx.cs" Inherits="Data.Dashboard" %>
<%@ Register src="../userControls/ucDropdownMenu.ascx" tagname="ucDropdownMenu" tagprefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<link rel="stylesheet" type="text/css" href="../Styles/paserMap.css" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
***CONTENT HERE***
<script src="../Scripts/Dashboard.js" type="text/javascript"></script>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)
以下是"../Script/Dashboard.js"文件的内容:
$(document).ready(function () {
$('.tgl:first').show(); // Show the first div
//Description: East panel parent tab navigation
$('.tabNav label').click(function () {
$('.tabNav li').removeClass('active')
$(this).parent().addClass('active');
var index = $(this).parent('li').index();
var divToggle = $('.ui-layout-content').children('div.tgl');
//hide all subToggle divs
divToggle.hide();
divToggle.eq(index).show();
});
});
Run Code Online (Sandbox Code Playgroud)
Dar*_*bio 190
晚到派对,类似于Briguy37的问题,但为了将来的参考,我使用以下方法并传递我想要推迟的函数,直到加载jQuery:
function defer(method) {
if (window.jQuery) {
method();
} else {
setTimeout(function() { defer(method) }, 50);
}
}
Run Code Online (Sandbox Code Playgroud)
它将每隔50ms以递归方式调用延迟方法,直到window.jQuery
它退出并调用为止method()
匿名函数的示例:
defer(function () {
alert("jQuery is now loaded");
});
Run Code Online (Sandbox Code Playgroud)
mal*_*lko 18
您可以使用defer属性在真正的末尾加载脚本.
<script type='text/javascript' src='myscript.js' defer='defer'></script>
Run Code Online (Sandbox Code Playgroud)
但通常以正确的顺序加载脚本应该可以解决问题,所以一定要在自己的脚本之前加入jquery
如果您的代码在页面中而不在单独的js文件中,那么您必须在文档准备好之后执行脚本,并且像这样封装代码也应该起作用:
$(function(){
//here goes your code
});
Run Code Online (Sandbox Code Playgroud)
thd*_*oan 15
另一种方法是这样做,尽管Darbio的推迟方法更灵活.
(function() {
var nTimer = setInterval(function() {
if (window.jQuery) {
// Do something with jQuery
clearInterval(nTimer);
}
}, 100);
})();
Run Code Online (Sandbox Code Playgroud)
San*_*der 12
编辑
你能为脚本标签尝试正确的类型吗?我看到你使用text/Scripts
,这不是javascript的正确mimetype.
用这个:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="../Scripts/jquery.dropdownPlain.js"></script>
<script type="text/javascript" src="../Scripts/facebox.js"></script>
Run Code Online (Sandbox Code Playgroud)
结束编辑
或者你可以看看require.js,它是你的javascript代码的加载器.
根据你的项目,这可能有点矫枉过正
moi*_*rex 12
最简单,最安全的方法是使用这样的东西:
var waitForJQuery = setInterval(function () {
if (typeof $ != 'undefined') {
// place your code here.
clearInterval(waitForJQuery);
}
}, 10);
Run Code Online (Sandbox Code Playgroud)
Nig*_*ist 11
你可以试试onload事件.加载所有脚本时引发:
window.onload = function () {
//jquery ready for use here
}
Run Code Online (Sandbox Code Playgroud)
但请记住,您可以覆盖使用window.onload的其他脚本.
我发现建议的解决方案仅在考虑异步代码时才有效。这是在两种情况下都可以使用的版本:
document.addEventListener('DOMContentLoaded', function load() {
if (!window.jQuery) return setTimeout(load, 50);
//your synchronous or asynchronous jQuery-related code
}, false);
Run Code Online (Sandbox Code Playgroud)
用:
$(document).ready(function() {
// put all your jQuery goodness in here.
});
Run Code Online (Sandbox Code Playgroud)
查看更多信息:http : //www.learningjquery.com/2006/09/introducing-document-ready
注意:只要您的 JQuery 库的脚本导入高于此调用,这应该可以工作。
更新:
如果由于某种原因你的代码没有同步加载(我从来没有遇到过,但显然可能从下面的评论不应该发生),你可以像下面这样编码。
function yourFunctionToRun(){
//Your JQuery goodness here
}
function runYourFunctionWhenJQueryIsLoaded() {
if (window.$){
//possibly some other JQuery checks to make sure that everything is loaded here
yourFunctionToRun();
} else {
setTimeout(runYourFunctionWhenJQueryIsLoaded, 50);
}
}
runYourFunctionWhenJQueryIsLoaded();
Run Code Online (Sandbox Code Playgroud)
这是一个常见的问题,想象一下你使用了一个很酷的PHP模板引擎,所以你有了你的基本布局:
HEADER
BODY ==> dynamic CONTENT/PAGE
FOOTER
Run Code Online (Sandbox Code Playgroud)
当然,你在某处阅读最好在页面底部加载Javascript,因此你的动态内容不知道谁是jQuery(或$).
另外你在某处阅读内联小Javascript很好,所以想象你需要在页面中使用jQuery,baboom,$未定义(..还是^^).
我喜欢Facebook提供的解决方案
window.fbAsyncInit = function() { alert('FB is ready !'); }
Run Code Online (Sandbox Code Playgroud)
所以作为一个懒惰的程序员(我应该说一个优秀的程序员^^),你可以使用等效的(在你的页面内):
window.jqReady = function() {}
Run Code Online (Sandbox Code Playgroud)
并在jQuery包含之后添加在布局的底部
if (window.hasOwnProperty('jqReady')) $(function() {window.jqReady();});
Run Code Online (Sandbox Code Playgroud)
setTimeout
您可以使用窗口本身中jQuery对象的定义作为钩子来执行依赖它的代码,而不是"等待"(通常使用).这可以通过使用定义的属性定义来实现Object.defineProperty
.
(function(){
var _jQuery;
Object.defineProperty(window, 'jQuery', {
get: function() { return _jQuery; },
set: function($) {
_jQuery = $;
// put code or call to function that uses jQuery here
}
});
})();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
115113 次 |
最近记录: |