的JavaScript / AJAX不工作在Opera,可以完美运行在FF / IE / Chrome浏览器 [英] Javascript/AJAX not working in Opera, works perfect in FF/IE/Chrome
问题描述
目前,我有这个JavaScript在名为getresults.js:
I currently have this Javascript in a file named getresults.js:
function getItems(str)
{
if (str=="")
{
document.getElementById("getItems").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("getItems").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/include/retrieveitems.php?q="+str,true);
xmlhttp.send();
}
这就是所谓的在此事件:
It's called upon by this event:
onclick="getItems('all')"
它完美的Firefox,IE,Chrome浏览器..但歌剧拒绝工作。我的观众的一个非常小的比例是Opera用户,但仍..我宁愿有它的工作。一个活生生的URL可以在这里找到: http://tf2g.com/gallery
如果任何人都可以帮助,非常感谢!
If anyone can help, much obliged!
推荐答案
这之所以失败,是事件处理函数不是调用你的getItems()方法在所有。它的看到document.getItems()方法从Opera的微观数据支持( http://www.whatwg.org/specs/web-apps/current-work/multipage/microdata.html ),并调用,来代替。这是JavaScript的范围的问题:这两个元素本身和它的文件范围,所以这里定义的方法/属性就可以隐藏你在全球范围内界定方法/属性
The reason this fails is that the event handler is not calling your getItems() method at all. It sees the document.getItems() method from Opera's Microdata support ( http://www.whatwg.org/specs/web-apps/current-work/multipage/microdata.html ) and calls that instead. It's a question of JavaScript scope: both the element itself and its document is in scope, so methods/properties defined here will be able to hide methods / properties you define in the global scope.
高兴的是,歌剧年初实施微观数据,所以你注意到了这一点:)
Be happy that Opera implemented Microdata early so you noticed this :)
最简单的解决办法是重命名功能,可避免使用微名称冲突。您也可以在标记使用的addEventListener(),而不是写的onclick = - 如果你的函数的范围是它创造了范围,这样你就不会遇到这样的陷阱
The simplest fix is to rename your function to avoid name collision with Microdata. You can also use addEventListener() instead of writing onclick="" in the markup - if you do the scope of the function is the scope it is created in, so you don't run into such gotchas.
window.addEventListener('load', function(){
for( var i=0,l;l=document.links[i]; i++ )if( l.hash){
l.addEventListener( 'click', function(){
getItems(this.hash.substr(1));
}, false);
}
}, false);
这篇关于的JavaScript / AJAX不工作在Opera,可以完美运行在FF / IE / Chrome浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!