/    Sign up×
Community /Pin to ProfileBookmark

appendChild() issue

Hi dudes. I’m trying to write an Ajax call using an iframe. (on purpose). However I seem to have quite the dificulty appending the iframe to the body element. Here is my script:

[CODE]function createFrame() {
var bodyElement = document.getElementsByTagName(“body”);

alert(bodyElement);

try {
var frameElement = document.createElement(“<iframe id=’xClBrCommFrame’ style=’display: none;’></iframe>”);
} catch(e) {
var frameElement = document.createElement(“iframe”);
frameElement.setAttribute(“id”, “xClBrCommFrame”);
frameElement.setAttribute(“style”, “display: none;”);
}

alert(bodyElement);

bodyElement.appendChild(frameElement);
}[/CODE]

It’s been driving me insane for two days now and I can’t figure it out even though I have used the same method, even code, before. Go figure…

Anyway, I keep getting a JS error from FF saying: “bodyElement.appendChild is not a function”

Please HALP!!!

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@felgallMay 24.2007 — var bodyElement = document.getElementsByTagName("body")[b][0][/b];
Copy linkTweet thisAlerts:
@bulgarian388authorMay 24.2007 — Tried that. Then I get a "bodyElement has no properties" error.
Copy linkTweet thisAlerts:
@Jeff_MottMay 24.2007 — First, you can use "document.body" instead. And second, make sure that the createFrame function isn't being called until the window has been fully loaded.

And an extra third, the code in the try block is unnecessary. You'll be fine with just the code in the catch block.
Copy linkTweet thisAlerts:
@bulgarian388authorMay 24.2007 — And an extra third, the code in the try block is unnecessary. You'll be fine with just the code in the catch block.[/QUOTE]

Really? I thought you were supposed to do it that way because IE can't cope with setAttribute().
Copy linkTweet thisAlerts:
@Jeff_MottMay 24.2007 — That's true for the NAME attribute only. Everything else works normally.
Copy linkTweet thisAlerts:
@bulgarian388authorMay 24.2007 — ...Are you serious. Freaking A, I wish I knew that before. I wrote a script once that was 1400 lines long and 1/2 of it was a redundant try/catch statement. Oh well. I'll try your example.
×

Success!

Help @bulgarian388 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 5.27,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...