在Firefox中使用javascript时出错

Ren*_*ary 3 html javascript undefined

我在Firefox中运行JavaScript时遇到问题.下面的脚本在除Firefox之外的其他浏览器中运行没有问题.

var vars = [], hash;
                var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

                for(var i = 0; i < hashes.length; i++)
                {
                    hash = hashes[i].split('=');
                    vars.push(hash[0]);
                    vars[hash[i]] = hash[1];
                }
                if (vars[0] != ' ')
                {
                    document.all['companyURL'].innerHTML = vars[0];
                    document.getElementById('domain').value = vars[0];
                }
Run Code Online (Sandbox Code Playgroud)

因此,此代码在页面加载期间运行,并且应该获取URL之后的值,并使用URL中的任何内容替换页面中的一行文本.

这是需要替换的文本行(yourcompany.com):

<h1><a href="" id="companyURL" name="companyURL">yourcompany.com</a> is available.<img src="images/checkmark_64.png" alt="check image"></h1>
Run Code Online (Sandbox Code Playgroud)

因此,如果网址是"google.com?hello.com",则页面中的文字需要从"yourcompany.com"更改为"hello.com",但是当在Firefox中加载页面时,它会给我错误" document.all未定义 "并指向包含此代码的代码行.

document.all['companyURL'].innerHTML = vars[0]; 
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会这样,我在网上找不到任何可以帮助我纠正问题的信息.请帮忙!

谢谢!

Jer*_*urg 7

更换:

document.all['companyURL'].innerHTML = vars[0];
Run Code Online (Sandbox Code Playgroud)

有:

document.getElementById('companyURL').innerHTML = vars[0];
Run Code Online (Sandbox Code Playgroud)


Dav*_*ean 6

mozilla/FF不支持document.all

你可以在document.getElementById("companyURL")那里使用