jquery yepnope 两次加载同一文件时抛出错误
$("#register").click(function()
{
yepnope({
load: ['js/join.js', 'css/join.css', 'css/join_form.css'],
});
$('#midcol').load('join.htm');
});
单击注册链接时,yepnope 会加载按预期列出的那些文件。当注册链接被点击第二次、第三次、第四次等等时...时间 yepnope 抛出以下内容 错误:
无法转换 JavaScript 参数 arg 0 [nsIDOMHTMLHeadElement.insertBefore] js/yepnope/yepnope.js 第 248 行
我的印象是 yepnope 仅在文件不存在时才加载该文件?我是否需要对要使用 yepnope 加载的每个文件进行检查?如果是这样的话,这似乎有点令人畏惧。
我在网上搜索了几个小时后找不到问题的解决方案。然而,我确实遇到了 CURL.js。它的占地面积相当小。我能够毫无问题地加载我的依赖项。下面是加载依赖项的代码示例。
var cnf = {
baseUrl: 'scripts',
pluginPath: 'curl/plugin',
paths: {
curl: 'curl/src/curl',
},
};
curl(cnf, ['js!join.js!order', 'domReady!'], function () {
$("#register").click(function(){
$("#midcol").load("join.htm");
});
});
$("#register").click(function()
{
yepnope({
load: ['js/join.js', 'css/join.css', 'css/join_form.css'],
});
$('#midcol').load('join.htm');
});
When the register link is clicked yepnope loads those files listed as expected. When the register link is clicked a second, third, fourth etc... time yepnope throws the following
error:
Could not convert JavaScript argument arg 0 [nsIDOMHTMLHeadElement.insertBefore]
js/yepnope/yepnope.js
Line 248
I was under the impression that yepnope will only load the file if it does not exist? Am I required to run a check on every file that I want to load with yepnope? That will seem a bit daunting if that is the case.
I could not find a solution to my issue while searching the web for HOURS. I did however, come across CURL.js. It has a rather smaller footprint. I was able to load my dependencies without any issues. Below is an example of code to load dependencies.
var cnf = {
baseUrl: 'scripts',
pluginPath: 'curl/plugin',
paths: {
curl: 'curl/src/curl',
},
};
curl(cnf, ['js!join.js!order', 'domReady!'], function () {
$("#register").click(function(){
$("#midcol").load("join.htm");
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我的经验,是的,如果脚本有可能被加载两次,您确实必须运行检查。对我来说,这似乎是一个相当罕见的案例。
在我目前正在处理的网站上,当用户在通过 AJAX 加载的几个部分之间来回切换并且每个部分都有自己的一组脚本时,我确实遇到了这种情况。用户可以先加载任一部分。
我将我的脚本设置为模块,因此执行此操作非常容易:
您绝对是对的,自动加载处理此操作会更方便。
In my experience, yes you do have to run a check, if there is the possibility of the script being loaded twice. That seems like a fairly rare case to me.
On the site I'm working on at the moment I did encounter this situation when the user switches back and forth between a couple of sections that are loaded via AJAX and each have their own set of scripts. The user could load either section first.
I have my scripts set up as modules so it's pretty easy to do this:
You're definitely right that having load handle this automatically would be much more convenient.