Error with javascript in firefox

≡放荡痞女 提交于 2019-12-07 19:09:22

问题


I have a problem with JavaScript running in Firefox. The script below runs without a problem in other browsers except 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];
                }

So this code runs during page load and should grab the values after the URL and replace a line of text in the page with whatever is in the URL.

This is the line of text it needs to replace (yourcompany.com):

<h1><a href="" id="companyURL" name="companyURL">yourcompany.com</a> is available.<img src="images/checkmark_64.png" alt="check image"></h1>

So if the url is "google.com?hello.com", then the text in the page needs to change from "yourcompany.com" to "hello.com", but when the page loads in Firefox, it gives me the error "document.all is undefined" and points to the line of code with this in it.

document.all['companyURL'].innerHTML = vars[0]; 

I have no idea why this is happening and I can't find any information online that can help me correct the issue. Please help!

Thanks!


回答1:


replace:

document.all['companyURL'].innerHTML = vars[0];

with:

document.getElementById('companyURL').innerHTML = vars[0];



回答2:


document.all is not supported by mozilla/FF

you can just use document.getElementById("companyURL") there




回答3:


document.all is an old IE4 standard. You should be using document.getElementById()

I would recommend you leverage one of the common javascript libraries that will abstract the different browsers implementations.



来源:https://stackoverflow.com/questions/4820440/error-with-javascript-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!