PDA

View Full Version : My DOM JS doesn't work in IE or Safari!


gigantorTRON
01-24-2008, 11:00 PM
I have javascript code as defined here:

<SCRIPT LANGUAGE="JavaScript">

//create onDomReady Event
window.onDomReady = DomReady;

//Setup the event
function DomReady(fn)
{
//W3C
if(document.addEventListener)
{
document.addEventListener("DOMContentLoaded", fn, false);
}
//IE
else
{
document.onreadystatechange = function(){readyState(fn)}
}
}

//IE execute function
function readyState(fn)
{
//dom is ready for interaction
if(document.readyState == "completed")
{
fn();
}
}

function hideStates(element, name)
{
if (name =='post_customer_Country')
{
if (element=='US' || element=='CA')
{
document.getElementById("state1").innerHTML = '<?php $HTML->print_StatesDorpDownBox("post_customer_State", "", $post_customer_State); ?>'
}

else
{
document.getElementById("state1").innerHTML='<input type="text" name="post_customer_State" id="post_customer_State_Text" size="15" maxlength="25" value="<?php print $post_customer_State; ?>">';
}
}
else
{
if (element=='US' || element=='CA')
{
document.getElementById("state2").innerHTML = '<?php $HTML->print_StatesDorpDownBox("post_members_State", "", $post_members_State); ?>'

}
else
{
document.getElementById("state2").innerHTML='<input type="text" name="post_members_State" id="post_members_State_Text" size="15" maxlength="25" value="<?php print $post_members_State; ?>">';
}
}
}

function onReady()
{
hideStates(document.getElementById('post_customer_Country').value, 'post_customer_Country');
hideStates(document.getElementById('post_members_Country').value, 'post_members_Country');
}

window.onDomReady(onReady);

</script>

Basically, this is a edit registration form containing country and state/province/region. If the United States or Canada is selected, a drop down box will appear for state. Otherwise, a text box will be displayed. However, I need the appropriate form element to be displayed upon loading which is what the onReady function is for. This works correctly in Firefox but not IE or Safari. I debugged in Safari and found no errors. I also ran the script through a JS validator.
:confused:

Thanks!

A1ien51
01-24-2008, 11:52 PM
Reason you are using dom ready instead of onload?

Eric

gigantorTRON
01-25-2008, 12:46 AM
I guess just because I've heard warnings against onLoad.

If you don't mind, what would be the difference in code using onLoad? I'm a javascript n00b.

A1ien51
01-25-2008, 02:52 AM
you woulc call your onReady function

window.onload = function onReady;

I have not run to code to see what the issues are. Busy at the moment.

Eric

gigantorTRON
01-25-2008, 05:59 AM
I switched onDomReady to onload and the code still does not work in either browser.
Any more ideas?

Thanks.

A1ien51
01-25-2008, 06:32 AM
Are you getting any JavaScript error messages?
If you view the page source, does the code look okay?

Is there any other onload handlers on the page?
If you add alert statements, can you see if they are being called correctly?

Eric

gigantorTRON
01-29-2008, 01:05 AM
No error messages, the code does look right, no other handlers and alerts are called correctly.
Everything works perfectly in Mozilla...