/    Sign up×
Community /Pin to ProfileBookmark

dynamic iframe element

I cannot access the attributs of iframe element which is just created on fly, any suggestion will be appreciate.

var ifr = document.createElement(‘iframe’);
ifr.setAttribute(“id”,”executeplace2″);
ifr.setAttribute(“name”,”executeplace2″);
ifr.setAttribute(‘src’,”);
ifr.style.border=’0px’;
ifr.style.width=’0px’;
ifr.style.height=’0px’;
document.body.appendChild(ifr);
alert(document.frames[“executeplace2”].name);
//can not access the name attribute

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@KorAug 23.2004 — objects are to be referenced mainly by

their id

their name

their tag name and their index (as array element)

I don't see why you need both id and name as identifier, but, well, here's the answer to your question:

alert(document.getElementById('executeplace2').name);
Copy linkTweet thisAlerts:
@williamhongauthorAug 23.2004 — Thank you, but I really need make sure the name is assigned properly,

actually the iframe created before is used as a target of a form, the form's src attribute will be set later like this:

document.form1.src = "executeplace2";

But I cannot find a way which make form's src to point to a frame without the name of the frame.
Copy linkTweet thisAlerts:
@KorAug 23.2004 — form's src ? I don't get it. First you say about an iframe now you are talking a bout a form... What src for a form? Can you be more explicit? What are you trying to build?
Copy linkTweet thisAlerts:
@williamhongauthorAug 23.2004 — I'm sorry, the src should be target,

The goal is building a dynamic invisible iframe which make the form could post to.
Copy linkTweet thisAlerts:
@KorAug 23.2004 — As I said, you may refere an object by it's id or by it's name also... If you don't want name, use id... I still don't get it, Which is really now your problem? Was not my answer

alert(document.getElementById('executeplace2').name);

show you how to refere the object?
Copy linkTweet thisAlerts:
@Willy_DuittAug 23.2004 — Your problem is not with the name or the id....

The problem is you can not reference the src after the iframe is appended to the document... You will need to use either location or href...

<i>
</i>&lt;script&gt;
var ifr = document.createElement('iframe');
ifr.setAttribute("id","executeplace2");
ifr.setAttribute("name","executeplace2");
ifr.setAttribute('src','C:\');
ifr.style.border='0px';
ifr.style.width='100px';
ifr.style.height='100px';
document.body.appendChild(ifr);
alert(document.frames["executeplace2"].src);
alert(document.frames["executeplace2"].location);

//can not access the name attribute
&lt;/script&gt;


.....Willy
Copy linkTweet thisAlerts:
@williamhongauthorAug 23.2004 — Please switch the "wrong line" and "right line" to see different the effect.


<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

</head>

<SCRIPT LANGUAGE="JavaScript">

function thissubmit(form)

{

var ifr = document.createElement('iframe');

ifr.setAttribute("id","executeplace2");

ifr.setAttribute("name","executeplace2");

ifr.style.border='0px';

ifr.style.width='0px';

ifr.style.height='0px';

document.body.appendChild(ifr);

document.form1.target = "executeplace2";//wrong line

//document.form1.target = "executeplace";//right line

document.form1.submit();

}

</SCRIPT>

<body>

<form name="form1" method="post" action="">

<p>

<input type="text" name="textfield">

</p>

<p>

<input type="button" name="Submit" value="Submit" onClick="thissubmit()">

</p>

</form>

</body>

<IFRAME NAME="executeplace" WIDTH="0" HIEGHT="0" FRAMEBORDER="0"></IFRAME>

</html>
×

Success!

Help @williamhong 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,
)...