/    Sign up×
Community /Pin to ProfileBookmark

firefox/ie compatibility issues with appendChild

Hello,

I am trying to use javaScript to get some links and titles from a blog on my site, and post them to a table on another page. I’ve used HTTP GET and then parsed out the links I want and post them by creating an anchor node.

This works perfectly in Firefox 3.0.4 and displays a table with 5 links. However, in IE, the table is blank. Is there anything I am not doing here?

Thank you for any advice.

Adam

[code]<script type=’text/javascript’>
var req=null;
var console, consol1, console2, console3, console4=null;
var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;

function sendRequest(url, params, HttpMethod) {
url=”http://niche.uwo.ca/blog/77″;
if (!HttpMethod) {
HttpMethod=”GET”;
}
req=initXMLHTTPRequest();
if (req) {
req.onreadystatechange=onReadyState;
req.open(HttpMethod, url, true);
req.setRequestHeader
(“Content-Type”, “application/x-www-form-urlencoded”);
req.send(params);
}
}

function initXMLHTTPRequest() {
var xRequest=null;
if (window.XMLHttpRequest) {
xRequest=new XMLHttpRequest();
} else if (window.ActiveXObject) {
xRequest=new ActiveXObject
(“Microsoft.XMLHTTP”);
}
return xRequest;
}

function onReadyState() {
var ready = req.readyState;
var data=null;
if (ready == READY_STATE_COMPLETE) {
data = req.responseText;
var justHeadline, justTitle, justNode, findNode, findTitle;
var dataArray = data.split(‘<h2>’);
for (var i = 2; i < 7; i++) {
justHeadline = dataArray[i];
justHeadline = justHeadline.substr(0, justHeadline.indexOf(“</h2>”));
findNode = justHeadline.indexOf(“” title=”);
justNode = justHeadline.substr(10, findNode-10);
justNode = “/” + justNode;
findTitle = justHeadline.indexOf(“”>”);
justTitle = justHeadline.substr(findNode + 9, findTitle-findNode-9);
justTitle = (” ” + justTitle);
if (console!= null) {
var newline=document.createElement(“a”);
newline.setAttribute(‘href’, justNode);
var txt=document.createTextNode(justTitle);
newline.appendChild(txt);
if (i == 2) {console.appendChild(newline);} else if (i == 3) {console1.appendChild(newline);} else if (i == 4) {console2.appendChild(newline);} else if (i == 5) {console3.appendChild(newline);} else if (i == 6) {console4.appendChild(newline);
}
}
}
}
}

window.onload=function(){
console=document.getElementById(‘console’);
console1=document.getElementById(‘console1’);
console2=document.getElementById(‘console2’);
console3=document.getElementById(‘console3’);
console4=document.getElementById(‘console4’);
sendRequest(“data.txt”);
}
</script>[/code]

The 5 titles and their links are then outputted in a table:

[code]<table>
<tbody>
<tr><td id=”console”></td></tr>
<tr><td id=”console1″></td></tr>
<tr><td id=”console2″></td></tr>
<tr><td id=”console3″></td></tr>
<tr><td id=”console4″></td></tr>
</tbody></table>[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@toicontienDec 10.2008 — Internet Explorer might be bombing out on the newline.setAttribute() function call.
[CODE]newline.href = justNode;[/CODE]
Copy linkTweet thisAlerts:
@KorDec 10.2008 — You should use standard javascript and refer correctly the elements upon their id using getElementById() method but giving different values for the variables (not the same as the ids):
<i>
</i>...
var cons=document.getElementById('console');
...
cons.appendChild(newline)
...

Same for the other elements
Copy linkTweet thisAlerts:
@dragleDec 10.2008 — Typo:
var console, consol[COLOR="Red"]e[/COLOR]1, console2, console3, console4;
Though if possible I recommend moving your console var definitions within your functions (still using var), at least within the onReadyState handler, instead of defining them as globals in the onload.

HTH
Copy linkTweet thisAlerts:
@acrymbleauthorDec 10.2008 — Thank you all for the suggestions. The typo in the variable declaration fixed the major glitch.
×

Success!

Help @acrymble spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...