【发布时间】:2011-10-10 21:51:54
【问题描述】:
我正在向其他网站提供 Javascript 小部件,而我在小部件中使用的 Jquery 似乎与主机站点对 Prototype 的使用相冲突。
您可以在此处查看实际发生的冲突: http://www.phillyrealestateadvocate.idxco.com/idx/8572/results.php?lp=100000&hp=500000&sqFt=0&bd=2&ba=0&searchSubmit=&city%5B%5D=131
在页面的右侧,单击绿色的“提问”按钮 - 它会生成一个弹出窗口,一旦您打开弹出窗口,“无效数组长度”错误就会开始滚动到 JS 控制台(并且不要停止。)然后弹出窗口无法关闭但仍然可以拖动。弹出窗口中的代码/内容位于 iframe 中,因此它仍然可以正常工作,但弹出窗口不会关闭。
Firebug 给我的错误是:
invalid array length 在 Prototype.js 中,但是当我展开详细信息时,它引用了 jquery.min.js,所以这让我相信两者是冲突的。
我的小部件代码完全在一个匿名函数中,并使用 Alex Marandon 描述的模型加载 Jquery: http://alexmarandon.com/articles/web_widget_jquery/
我正在使用 noConflict 调用,但它可能没有按照我放置的方式工作?
这是我的小部件脚本的开头,其中包含对 jquery.min.js 和 jqueryui 的引用:
(function() {
// Localize jQuery variable
var jQuery;
/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.6.2')
{
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src",
"http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js");
script_tag.onload = scriptLoadHandler;
script_tag.onreadystatechange = function () { // Same thing but for IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
scriptLoadHandler();
}
};
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
}
else
{
$.getScript( "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js" ,
function()
{
// The jQuery version on the window is the one we want to use
jQuery = window.jQuery;
main();
}
);
}
/******** Called once jQuery has loaded ******/
function scriptLoadHandler()
{
$.getScript( "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js" ,
function()
{
// Restore $ and window.jQuery to their previous values and store the
// new jQuery in our local jQuery variable
jQuery = window.jQuery.noConflict(true);
main();
}
);
}
/******** Our main function ********/
function main()
{
// Do a bunch of stuff here
}
})(); // We call our anonymous function immediately
任何帮助将不胜感激!
编辑:我现在在小部件中直接拥有 Jquery UI,因此我完全消除了 getScript 调用。不幸的是,这并没有解决冲突。这是新代码:
(function() {
// Localize jQuery variable
var jQuery,
/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.6.2')
{
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js");
script_tag.onload = scriptLoadHandler;
script_tag.onreadystatechange = function () { // Same thing but for IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
scriptLoadHandler();
}
};
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
}
else
{
jQuery = window.jQuery;
main();
}
/******** Called once jQuery has loaded ******/
function scriptLoadHandler()
{
jQuery = window.jQuery.noConflict(true);
main();
}
/******** Our main function ********/
function main()
{
【问题讨论】:
标签: javascript jquery jquery-ui prototypejs conflict