开发者

InnerHTML doesn't work in FF?

i'm pretty new to Javascript and basiclly everything related to web coding. i have a simple problem using InnerHTML in FF, i hope you can help me.

i'm using this code, that should generate a simple html input line, and in IE it works fine (although when i load it i get the "should i enable activeX msg on top), but in FF it doesn't work at all, i can see it's on the page thorugh source, but it doesn't show anything...

<div id="mainDiv"></div>

<script type="text/javascript">
    var siteBoxes = '<form action=HTMLPage.htm name="myform">';
    for (var i = 0; i < arr1.length; i++) {
        siteBoxes += '<INPUT TYPE="checkbox" id="box'+i+'"

VALUE="'+arr1[i]+'"/>  '+arrNames[i]+'

'; } siteBoxes += ''; document.getElementById("mainDiv").innerHTML=siteBoxes;

i'm sure it's a simple solution, and i tried searching on the web, but i'm running out of strength for that, i hope any of you kind people can help me.

thanks in advance!!!

ok, the problem is with the array definition in the head. i just noticed that in the error console in FF i开发者_如何学JAVA get a msg that the arr1 is undefined, but it is, i even tried moving it to the body and it doesn't change, still undefined... and it works in IE.

could it be something with the array definition? is it different from IE and FF???

var arr1 = new Array(
         "http://www.google.com",
         "http://www.yahoo.com",
         "http://www.cnet.com",
         "http://www.google.ar/search?q="
         );

again, it works great in IE, but not in FF


Somethings I noticed at first glance.

<div id="mainDiv"></div>

<script type="text/javascript">
    var siteBoxes = '<form action="HTMLPage.htm" name="myform">';//put quotes around page
    for (var i = 0; i < arr1.length; i++) {
        siteBoxes += '<INPUT TYPE="checkbox" id="box'+i+'" VALUE="'+arr1[i]+'"\/> 
   &nbsp;'+arrNames[i]+'<br \/><br \/>';
}
siteBoxes += '<\/form>';
document.getElementById("mainDiv").innerHTML=siteBoxes;

arr1 is never declared at least from the code you present to us.


Works in Opera, IE and FF for me.

Try unescaping the output;

    document.getElementById("mainDiv").innerHTML = unescape(siteBoxes);


There is something wrong with the ending quotation mark in your script tag. If I delete it and type a new one, the code works.


There must be a problem with the rest of your code because when I change it. It works fine.

<html>
<head>
</head>
<body>
<div id="mainDiv"></div>
<script type="text/javascript">
    var arr1 = new Array();
    var arrNames = new Array();
    arr1[0] = "test";
    arrNames[0] = "nameTest";
    var siteBoxes = '<form action=HTMLPage.htm name="myform">';
    for (var i = 0; i < arr1.length; i++) {
        siteBoxes += '<INPUT TYPE="checkbox" id="box'+i+'" VALUE="'+arr1[i]+'"/>     &nbsp;'+arrNames[i]+'<br /><br />';
}
siteBoxes += '</form>';
document.getElementById("mainDiv").innerHTML = siteBoxes;
</script>
</body>
</html>


Here's perhaps a cleaner way of implementing this (see comments in code as well):

//You can use the simple way of creating an array, and instead of having two
//arrays that represent the names and urls, just make a single array of JSON
var sitesArray = [
    {siteName: "Google",siteUrl:"http://www.google.com"},
    {siteName: "Yahoo",siteUrl:"http://www.yahoo.com"},
    {siteName: "CNET",siteUrl:"http://www.cnet.com"},
    {siteName: "Google Search",siteUrl:"http://www.google.ar/search?q="}
];

//Create an ouput array where you'll compile your html
var outputArray = [];

//Now loop through sitesArray and push the strings onto the ouputArray 
for (var i=0,len=arr1.length;i < len;++i) {
     outputArray.push("<input type=\"checkbox\" id=\"box",i,"\" ",
                             "value="\",sitesArray[i].siteUrl,"\" />&nbsp",
                             sitesArray[i].siteName,"<br /><br />");
}

document.getElementById("mainDiv").innerHTML = outputArray.join("");

The primary reason for suggesting this is that string concatenation can be very slow, especially if you have lots of long strings. It's not as big an issue in Firefox, but it's definitely an issue in IE. So pushing strings onto an array, then joining them at the end will give you better performance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜