The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. February 12, 2012, 04:03:40 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Javascript/AJAX and IE problem  (Read 1435 times)
GatekeeperCruelman
n00b
*
Offline Offline

Posts: 2


View Profile
« on: March 17, 2009, 04:17:13 AM »

Hello all,
I've got an problem with AJAX and IE, maybe this is also interesting for you guys and gals.
This is just a test file I made.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<body>
<script type="text/javascript">
function FuncTest()
{
window.document.getElementById('ftest').innerHTML="Hello World!";
}
function funcTEST()
{
document.getElementById('test').innerHTML="<option>Hello World!</option>";
}
</script>
<form name="myForm">
<select id="test" name="test">
<option>Click first</option>
</select>
<input type="button" onclick="funcTEST()" value="Click" />
<br /><br />
<b>Div</b><br />
<input type="button" onclick="FuncTest()" value="Click" />
<div id="ftest" name="ftest"></div>
</form>
</body>
</html>

This works with every browser I tried except IE (I tried Firefox, Opera, Chrome, Safari).
When I press the button next to the combobox I should get a different list but for some reason IE doesn't do that.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<body>
<script type="text/javascript">
function FuncTest()
{
window.document.getElementById('ftest').innerHTML="Hello World!";
}
function funcTEST()
{
var x = document.getElementById('test');
x.options[0].text="Hello World!";
}
</script>
<form name="myForm">
<select id="test" name="test">
<option>Click first</option>
</select>
<input type="button" onclick="funcTEST()" value="Click" />
<br /><br />
<b>Div</b><br />
<input type="button" onclick="FuncTest()" value="Click" />
<div id="ftest" name="ftest"></div>
</form>
</body>
</html>

With this code it works, but how can make the same situation with the output from my php script I used in AJAX, or how can I solve the first problem?
Thanks in advanced,
GatekeeperCruelman
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 9792



View Profile
« Reply #1 on: March 17, 2009, 06:31:03 PM »

I think the fact that it works for browsers other than IE is because of tolerance, not an IE bug.

(Note this day, I've officially said that perhaps IE got it right and others did not. It's a BANNER day.)

I don't believe that it is wise to ever deal with the innards of a SELECT with innerHTML. I've not tried it, but it made furrowed my brow when I saw the code. You should always deal with SELECT options as objects hanging off it, like you did in the second case.

I am not entirely sure what you mean, but I think what you are asking is how to get a response from an AJAX query and put it into a select box.

The best way, IMO, is to json_encode on the php side and decode it as an array client side.

So in PHP, you'd create your array, encode it and send it back:
$arr[] = 'this';
$arr[] = 'is';
$arr[] = 'a';
$arr[] = 'test';
echo json_encode($arr);

... then in JS you'll need to convert that into an array object just so:

(assuming you have the returned string in your hands already as "retStr")

Code:
var myArr = eval('(' + retStr + ')');
var max = myArr.length;
var targetSelect = document.getElementById('theSelect');
targetSelect.options.length = max;

for (var i=0; i<myArr.length; i++)
{
targetSelect.options[i].value = myArr[i];
targetSelect.options[i].text = myArr[i];
}

... and you're all set. Hope that spins your gears a bit.
« Last Edit: March 23, 2009, 11:10:05 AM by perkiset » Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
GatekeeperCruelman
n00b
*
Offline Offline

Posts: 2


View Profile
« Reply #2 on: March 23, 2009, 06:56:39 AM »

Thanks ^^
I solved it in another way with IE, but this is interesting.
I'll take a look at it.
Thanks again ^^
Logged

No links in signatures please
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #3 on: March 27, 2009, 09:37:29 AM »

Perks is right. A lot of browsers will let you get away with modifying attributes of certain elements that they are not supposed to have, like innerHTML. Tables are a good example of this - they aren't supposed to allow you to touch innerHTML, they are supposed to be treated as DOM nodes with lots of subnodes (td, tr, etc).
Logged

hai
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!