PDA

View Full Version : Javascript compatibility Issue works in IE but not Firefox



drent
02-23-2010, 04:46 PM
Hello, I have a script that works fine in Internet explorer but does nothing in Firefox. Here is the code I am currently using:


<script type="text/javascript">

var ExternalSite = "http://www.readtheticker.com/?userID="

try {
function GetMemberID () {
var spans = document.getElementsByTagName('span');
for (var i = 0; i < spans.length;){
if (spans[i].innerText == "Member ID") {
var MemberID = spans[i+1].innerText;
var chkAdr = window.location.href;
if (chkAdr.indexOf("?getID") != -1) {
window.location = ExternalSite + MemberID;
break;
}
}
i++;
}
}
if (window.BonaPage) BonaPage.addPageStateHandler(BonaPage.PAGE_PARSED, GetMemberID);
} catch(err) {}
</script>

If anyone could help me out with the changes that need to be made I'd very much appreciate it!

jscheuer1
02-24-2010, 02:07 AM
Always check to be sure the properties and methods you are using are available in the browsers you want the script to run on.

For instance with:


addPageStateHandler

I've never heard of that one before. Unless it is defined elsewhere in code you haven't shown, it may be an IE proprietary function.

By the same token:


innerText

is (I believe) an IE only property. The closest cross browser equivalent would be:


innerHTML

which will work in IE as well, and should do the job at least as far as that part goes.

To check if a particular command is available in a particular browser, Google may often be used to research that.

Also one can do something like:


alert(typeof object.property_or_method);

Like with innerText, one may see it is not supported in Firefox with this test:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
onload = function(){alert(typeof document.getElementsByTagName('span')[0].innerText);};
</script>
</head>
<body>
<span>Hi</span>
</body>
</html>

In IE it will give you:

string

In Firefox:

undefined