I have a script that is throwing an ‘object required’ error but only on the customer’s machines (in IE 7 and 8) … on my machine in IE 8 it works fine, on my wife’s machine (IE 7) it works fine, on my laptop (IE 6) it works fine … and it works in EVERY other browser out there (FF, Opera, Safari, Flock)…
The guy is making me crazy with this dumb thing and I am saying it has to be something on his machine because I see no problems, nor get any errors on any of my machines. Here is the code:
He said he shut off all the security settings. I also found some info about Vista screwing up things but haven’t heard back from him on whether he is using it.
Break your code up into many lines so the line number in the error message is more specific.
Instead of assuming an object has a property or method, test it before using it.
if (window.open) {
window.open(...);
}
var elem = document.getElementById('livesearch');
// elem could be null. trying to use a property of something that is null will throw an error
if (elem) {
// elem is not null, we can use the property
elem.value;
}
Be aware that an element with the given id needs to exist at the time that script runs for getElementById() to be able to grab it.
I was referring to the IE security settings at Tools > Internet Options > Security where you set the config settings for the Internet Zone, Trusted Sites, etc. I’m not aware of any ability to “shut those off”.
Every website belongs to one of the zones, and each zone has several dozen possible customizable security settings. Some relate to script-opened windows and opening windows with particular characteristics.
If he has something disabled that your code tries to do, then the code won’t work.
In addition, JavaScript (“active scripting”) might be disabled in his Internet Zone. His own site might be in Trusted Sites, where JS is enabled. If your script depends on any scripts called from outside sites, those sites are likely in the Internet Zone. Those scripts won’t run, and thus your script will crash because the objects it needs weren’t created. In other words, when a user is using the IE Zones to control scripts, all the sites that pieces of scripting come from must be in Trusted Sites; otherwise some of the pieces won’t run, which could crash the others.
Have you tested yourself, or has the client tested it? The only things in that code that can go wrong are the document.getElementById and window calls. Try this code instead:
function show_preview() {
var ids = ['livesearch', 'subject', 'message'];
for(var i = 0; i < ids.length; i++) {
var id = ids[i]
this[id] = document.getElementById(id);
if(!this[id]) {
this[id] = '';
// Might want to do some error handling here, since the element doesn't exist
} else {
this[id] = this[id].value;
}
}
var url = "c_pop.php?to="
+ livesearch
+ "&sub="
+ subject
+ "&mess="
+ message;
window.open(url, "Link", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=450,height=400,left=30,top=80");
}