为什么window.load()在firefox中工作,但在chrome中不工作
Why window.load() is working in firefox but not in chrome?
我是一个新手,所以请原谅我的知识不足。使用JQuery,我试图在树中显示两个xml文件的数组。代码很简单,这是我的html页面:
window.onload = openXML('xml_files/categories.xml','xml_files/products.xml');
var categories = new Array();
var products = new Array();
function jquery_treeview()
{
var unorderedList= "";
console.log(categories.length);
for (i=0; i < categories.length; i++)
{
unorderedList += "<li ><span onclick='showCategoryDetails("+i+")'>" + categories[i][1] + "</span><ul><ol type='a'>";
for (j=0; j < products.length; j++)
{
if (categories[i][0] == products[j][2]){
unorderedList += "<li ><span onclick='showProductDetails("+j+")'>" + products[j][1] +"</span></li>";}
}
unorderedList += "</ul></li>";
}
var jquerytreeview = $(unorderedList).appendTo("#treeViewRight");
$("#treeViewRight").treeview({
add: jquerytreeview
});
}
function showCategoryDetails(index)
{
var details = "Category: " + categories[index][1]+ "'n" + "Description: " + categories[index][2];
alert(details);
}
function showProductDetails(index)
{
var details = "Product: " + products[index][1] + "'n" + "Price: " + products[index][4]+ "'n" + "Quantity per Unit: " + products[index][3];
alert(details);
}
function openXML(XMLcategory, XMLproduct)
{
var getCategory, getProduct;
if (window.XMLHttpRequest)
{
getCategory=new XMLHttpRequest();
getProduct=new XMLHttpRequest();
}
getCategory.onreadystatechange=function()
{
if (getCategory.readyState == 4 && getCategory.status == 200)
{
category = getCategory.responseXML.documentElement.getElementsByTagName("Categories");
for (i=0; i < category.length; i++)
{
var categoryArray = new Array();
count=0;
categoryArray[count++]=category[i].getElementsByTagName("CategoryID")[0].firstChild.nodeValue;
categoryArray[count++]=category[i].getElementsByTagName("CategoryName")[0].firstChild.nodeValue;
categoryArray[count++]=category[i].getElementsByTagName("Description")[0].firstChild.nodeValue;
categories[i]=categoryArray;
}
}
}
getProduct.onreadystatechange=function()
{
if (getProduct.readyState == 4 && getProduct.status == 200)
{
product = getProduct.responseXML.documentElement.getElementsByTagName("Products");
for (i=0; i < product.length; i++)
{
var productArray = new Array();
count=0;
productArray[count++]=product[i].getElementsByTagName("ProductID")[0].firstChild.nodeValue;
productArray[count++]=product[i].getElementsByTagName("ProductName")[0].firstChild.nodeValue;
productArray[count++]=product[i].getElementsByTagName("CategoryID")[0].firstChild.nodeValue;
productArray[count++]=product[i].getElementsByTagName("QuantityPerUnit")[0].firstChild.nodeValue;
productArray[count++]=product[i].getElementsByTagName("UnitPrice")[0].firstChild.nodeValue;
products[i]=productArray;
}
}
}
getCategory.open("GET", XMLcategory, true);
getCategory.send();
getProduct.open("GET", XMLproduct, true);
getProduct.send();
}
jquery.treeview.js文件从这里下载
我的问题是它在Firefox上工作(即使我每次重新加载时都要清除历史记录),但它在Chrome或IE上不起作用。我希望有人能帮助我。谢谢你。
您有一个竞争条件。您有三个异步操作,两个用于获取数据,一个用于显示树视图。
如果显示树形视图的操作不是最后一个,则它没有显示树形视图所需的数据。
当获取数据的两个操作完成时,您应该显示树视图。您可以在每个readystatechange
处理程序的末尾检查其他处理程序是否已经完成。
当您想要显示树视图时,如果文档尚未加载,则需要等待文档加载。在jQuery中使用ready
事件可以解决这个问题,因为如果还没有加载,它将等待文档加载,如果文档已经加载,则立即调用该函数。
function showTreeView() {
$(document).ready(jquery_treeview);
}
getCategory.onreadystatechange=function()
{
if (getCategory.readyState == 4 && getCategory.status == 200)
{
category = getCategory.responseXML.documentElement.getElementsByTagName("Categories");
for (i=0; i < category.length; i++)
{
var categoryArray = new Array();
count=0;
categoryArray[count++]=category[i].getElementsByTagName("CategoryID")[0].firstChild.nodeValue;
categoryArray[count++]=category[i].getElementsByTagName("CategoryName")[0].firstChild.nodeValue;
categoryArray[count++]=category[i].getElementsByTagName("Description")[0].firstChild.nodeValue;
categories[i]=categoryArray;
}
if (products.length) showTreeView();
}
}
getProduct.onreadystatechange=function()
{
if (getProduct.readyState == 4 && getProduct.status == 200)
{
product = getProduct.responseXML.documentElement.getElementsByTagName("Products");
for (i=0; i < product.length; i++)
{
var productArray = new Array();
count=0;
productArray[count++]=product[i].getElementsByTagName("ProductID")[0].firstChild.nodeValue;
productArray[count++]=product[i].getElementsByTagName("ProductName")[0].firstChild.nodeValue;
productArray[count++]=product[i].getElementsByTagName("CategoryID")[0].firstChild.nodeValue;
productArray[count++]=product[i].getElementsByTagName("QuantityPerUnit")[0].firstChild.nodeValue;
productArray[count++]=product[i].getElementsByTagName("UnitPrice")[0].firstChild.nodeValue;
products[i]=productArray;
}
if (categories.length) showTreeView();
}
}
似乎您也尝试将load
事件用于openXML
函数。这是行不通的,因为它会在事件发生之前被jquery_treeview
引用覆盖。但是,您没有将openXML
函数设置为事件的事件处理程序,您正在调用该函数并将返回值设置为事件处理程序,并且由于该函数不返回函数,因此不做任何事情。
在开始加载数据之前不需要等待load
事件,只需直接调用该方法:
openXML('xml_files/categories.xml','xml_files/products.xml');
相关文章:
- JS可以在Chrome中工作,但不能在Firefox中工作
- jQuery Datepicker可以在Safari中工作,但不能在FF或Chrome中工作
- Javascript的某些部分在Chrome中不起作用,但在其他浏览器中可以完美工作
- javascript window.open将无法在Chrome控制台上工作
- 如何编写在Chrome和IE中正常工作的JavaScript
- 只有当Javascript控制台在chrome上打开时,Javascript才能工作
- Chrome地理定位在更新后停止工作
- 地理定位无法在chrome中工作
- Json到CSV下载,可以在chrome中工作,但不能在IE浏览器中工作
- prettyPhoto无法在Chrome和IE9中工作
- window.location.htm无法在chrome扩展中工作
- jQuery$.post可以在chrome、safari中工作,但不能在FF中工作(声明成功回调函数未定义)
- Chrome没有加载最新版本的web工作程序脚本(运行缓存版本)
- HTML5视频无法在chrome上工作,在服务器上托管后的IE,mov文件中的视频(quicktime格式)
- 异步加载的SVG过滤器feColorMatrix在Chrome中工作,而不是在Safari或Firefox中
- JQuery 灯箱不工作.Chrome 在显示控制台中报告错误
- 如何使工作 Chrome 扩展程序的未捕获异常处理程序(由于 CORS 保护而不起作用)
- JavaScript表单脚本工作Chrome,但没有其他地方
- 锚标记下载属性不工作:Chrome 35.0.1916.114中的错误
- JQuery&子选择器A>B: 不工作?[Chrome 24,JQmobile]