Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript codes works on IE6 and FF but not on IE7

Status
Not open for further replies.

g5515

Technical User
Dec 6, 2007
3
Hi guys...

This is my first post. I really need help... very desperate already. The codes are really simple... and it only loads on the same page (required).
See code below:
Code:
<select id="A" name="mylist" onchange="history.go()">
<option value="1">First</option>
<option value="2">Second</option>
</select>

<script type="text/javascript">
var x = document.getElementById("A").value;
if (x == 1)
{
document.write('Displayed First');
}
else if (x == 2)
{
document.write('Displayed First');
}
</script>
Code:
This script perfectly runs on IE6 and Mozilla FireFox, but unfortunately not in IE7. It will only display the content of the initial list item.
 
oops.. sorry... codes are not enclosed:
Code:
<select id="A" name="mylist" onchange="history.go()">
      <option value="1">First</option>
      <option value="2">Second</option>
    </select>

<script type="text/javascript">
var x = document.getElementById("A").value;
if (x == 1)
{
    document.write('Displayed First');
}
else if (x == 2)
{
    document.write('Displayed First');
}
</script>
 
That's because IE7 doesn't set the selected value 'till after the page has loaded and your script executes before that, which means it will detect the default value, 1.

A solution would be to have the code execute onload, since IE will have set the field value by then, but you can't use that since using document.write after the page load results in entirely overwriting that page.

I'm not sure why you would have to use history.go() and document.write to display the content you need, but you might wanna look at stuff like innerHTML to dynamically change your content.
Code:
<select id="A" name="mylist" onchange="detect()">
      <option value="1">First</option>
      <option value="2">Second</option>
</select>
<div id="warning"></div>

<script type="text/javascript">
function warn (str) {
    document.getElementById("warning").innerHTML = str;
}
function detect() {
    var x = document.getElementById("A").value;
    if (x == 1)
    {
        warn('Displayed First');
    }
    else if (x == 2)
    {
        warn('Displayed Second');
    }
}
window.onload = detect;
 
Great... this is perfect! Thank you very much!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top