473,396 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Setting iframe name in script

I'm trying to use a script-generated form to submit to a
script-generated iframe. The problem I'm running into is that the
iframe is not assuming the name I assign it.

IE6 on Win2000. FF1.0.2+ doesn't seem to have the problem.

I'm doing:
this.iframe = document.createElement( 'iframe' );
this.iframe.id = this.id + 'wh';
this.iframe.style.display = 'none';
this.iframe.name = this.iframe.id; // <---

// snippage, including generation of the form

this.form.target = this.iframe.name;
this.form.appendChild( this.iframe );
However, when I submit the form it opens in a new window.
When I alert( document.body.innerHTML ), I am being told:

<IFRAME id=dr436929wh></IFRAME>
[sic]
i.e. no "name" attribute (hence the new window).
What I've already tried:
* Setting the iframe.name attribute only after
this.form.appendChild( this.iframe )

* Using this.iframe.setAttribute( name, ... ) prior to and after the
appendChild

* Setting this.form.target = this.iframe (just for the hell of it)
Thanks in advance. I know you guys are good. Let me know if you need
more info.

Jul 23 '05 #1
7 4815
Just use the "id" tag, and not the name tag. Refer to it as
dr436929wh.location.href = "Whatever". Or, you can assign it's name
like this: dr436929wh.name = "Something". Try that and see if it works.

Jul 23 '05 #2
Just use the "id" tag, and not the name tag. Refer to it as
dr436929wh.location.href = "Whatever". Or, you can assign it's name
like this: dr436929wh.name = "Something". Try that and see if it works.

Jul 23 '05 #3


Christopher J. Hahn wrote:
I'm trying to use a script-generated form to submit to a
script-generated iframe. The problem I'm running into is that the
iframe is not assuming the name I assign it.

IE6 on Win2000. FF1.0.2+ doesn't seem to have the problem.

I'm doing:
this.iframe = document.createElement( 'iframe' );
this.iframe.id = this.id + 'wh';
this.iframe.style.display = 'none';
this.iframe.name = this.iframe.id; // <---

// snippage, including generation of the form

this.form.target = this.iframe.name;
this.form.appendChild( this.iframe );
However, when I submit the form it opens in a new window.
When I alert( document.body.innerHTML ), I am being told:

<IFRAME id=dr436929wh></IFRAME>
[sic]
i.e. no "name" attribute (hence the new window).


This is a known and documented shortcoming of IE on Win that can be
quite crippling to any effort to elegantly use DOM scripting in the
browser. For some reasons setting the name property (e.g. element.name =
expression) or name attribute (e.g. element.setAttribute('name',
expression) on a dynamically created element does not work in IE/Win so
you are forced to use the very ugly IE specific workaround of doing
var iframe = document.createElement('<iframe name="iframeName">')
However some browsers (notably Mozilla) will throw an error on that
argument to createElement so you have to use try/catch e.g.
var iframeName = 'iframeName';
var iframe;
// try IE way first
try {
iframe = document.createElement('<iframe name="' + iframeName + '">');
}
catch (e) {
iframe = document.createElement('iframe');
iframe.name = iframeName;
}
// now deal with browsers like Opera with do not throw an error
// but might return null or an unusable element
if (!iframe || iframe.tagName.toLowerCase() != 'iframe' ||
iframe.name != iframeName) {
iframe = document.createElement('iframe');
iframe.name = iframeName;
}
// now use iframe element here further

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #4
ve***********@gmail.com wrote:
Just use the "id" tag, and not the name tag. Refer to it as
dr436929wh.location.href = "Whatever". Or, you can assign it's name
like this: dr436929wh.name = "Something". Try that and see if it works.


Micro: Thanks for the response. I tried a couple variants of your
suggestion but it refused to set the name attribute. Unfortunately,
this is a must as the form will often require POST for Apache logging
reasons. I ended up directly modifying the form's innerHTML as an IE
special case to put the iframe in there, name="..." included.

Thanks again.

Jul 23 '05 #5
Martin Honnen wrote:
Christopher J. Hahn wrote:
I'm trying to use a script-generated form to submit to a
script-generated iframe. The problem I'm running into is that the
iframe is not assuming the name I assign it.

IE6 on Win2000. FF1.0.2+ doesn't seem to have the problem. [snip]
However, when I submit the form it opens in a new window.
When I alert( document.body.innerHTML ), I am being told:

<IFRAME id=dr436929wh></IFRAME>
[sic]
i.e. no "name" attribute (hence the new window).
This is a known and documented shortcoming of IE on Win that can be
quite crippling to any effort to elegantly use DOM scripting in the
browser. For some reasons setting the name property (e.g. element.name =
expression) or name attribute (e.g. element.setAttribute('name',
expression) on a dynamically created element does not work in IE/Win so
you are forced to use the very ugly IE specific workaround of doing
var iframe = document.createElement('<iframe name="iframeName">')


Nice trick-- I like that one much better than what I ended up using.
I'm going to give this a shot because it's far more elegant than
appending the iframe tag to the form's innerHTML.

IE didn't have a problem setting the names of form fields, so I'm
wondering if this is a case of "If it has an ID, what's it need a name
for?" or whether form fields are a special case (for obvious reasons).
However some browsers (notably Mozilla) will throw an error on that
argument to createElement so you have to use try/catch e.g.
var iframeName = 'iframeName';
var iframe;
// try IE way first
try {
iframe = document.createElement('<iframe name="' + iframeName + '">');
}
catch (e) {
iframe = document.createElement('iframe');
iframe.name = iframeName;
}
// now deal with browsers like Opera with do not throw an error
// but might return null or an unusable element
if (!iframe || iframe.tagName.toLowerCase() != 'iframe' ||
iframe.name != iframeName) {
iframe = document.createElement('iframe');
iframe.name = iframeName;
}
// now use iframe element here further

--

Martin Honnen
http://JavaScript.FAQTs.com/


Excellent suggestion, and I will be trying it out. If you're
interested, I'll post the results of my attempts.

This what I'm using now (with impertinent code snipped):
this.id = 'dr' + Math.round( Math.random() * 1000000 );
this.iframe = document.createElement( 'iframe' );
this.iframe.id = this.id + 'wh';
this.iframe.style.display = 'none';
this.iframe.name = this.iframe.id;

this.form = document.createElement( 'form' );
this.form.target = this.iframe.name;
this.form.id = this.id + 'f';

if( document.all ) {
this.form.innerHTML += '<iframe id="' + this.iframe.id +
'" name="' + this.iframe.id +
'" style=display:none;></iframe>';

} else
this.form.appendChild( this.iframe );
Inelegant, but it works.

Jul 23 '05 #6


Christopher J. Hahn wrote:

This what I'm using now (with impertinent code snipped):
this.id = 'dr' + Math.round( Math.random() * 1000000 );
this.iframe = document.createElement( 'iframe' );
this.iframe.id = this.id + 'wh';
this.iframe.style.display = 'none';
this.iframe.name = this.iframe.id;

this.form = document.createElement( 'form' );
this.form.target = this.iframe.name;
this.form.id = this.id + 'f';

if( document.all ) {
this.form.innerHTML += '<iframe id="' + this.iframe.id +
'" name="' + this.iframe.id +
'" style=display:none;></iframe>';


I would change that to
if (this.form.insertAdjacentHTML) {
this.form.insertAdjacentHTML(
'beforeEnd',
'<iframe id="' + ... + '<\/iframe>'
)
}
else {
this.form.appendChild(this.iframe);
}
as IE's DOM (and even other browsers like Opera emulating IE's DOM) has
insertAdjacentHTML to efficiently parse a snippet of HTML and insert the
resulting nodes whereas your approach of innerHTML += requires reparsing
the existing content.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #7

Christopher J. Hahn wrote:
IE didn't have a problem setting the names of form fields, so I'm
wondering if this is a case of "If it has an ID, what's it need a name
for?" or whether form fields are a special case (for obvious reasons).


The problem with name exists for form controls too e.g. try

var form = document.createElement('form');
var input = document.createElement('input');
input.name = 'inputName';
form.appendChild(input);
alert(form.innerHTML)

with IE/Win and you see that the name attribute is not there. But
somehow IE manages to submit those form controls properly nevertheless
so you only run into problems when you want to access them by script
where you often get away with setting the id besides the name.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Russell | last post by:
I have a quirky issue that I believe involves timing and only 2 hairs left to pull. I have a modal dialog that is an IFrame. The IFrame contains another window - which contains the appropriate...
3
by: Bob | last post by:
I'm a total newbie at Javascript, but a programmer for over 20 years... so, my plans may be bigger than they are possible. I'm trying to set with Javascript the value of a form field... easy...
15
by: Aaron Gray | last post by:
<iframe name="iframe" width="100%" height="25%" src="test1.txt"> </iframe> <a href="test1.txt" target="input">one</a> <a href="test2.txt" target="input">two</a> <form name="form1"> <textarea...
3
by: Stevie_mac | last post by:
It might be me but... I dont seem to get a Page_Load event when a opening an ASPX in an iFrame. I do geta Page_Load event when an item on the ASPX (inside the iFrame) is clicked but then...
1
by: knkk | last post by:
Hi, I have an iframe that fetches content from a different domain. This is my URL, which has an iframe that fetches content from another site careerbuilder.co.in (this is a partnership channel, so...
2
riptide2049
by: riptide2049 | last post by:
I really have a problem here. I have a code that is suppost to take the href of a link from the right class;value of a link maked toreturn false. the value is a Media file the file is sent to...
0
by: tequilamala | last post by:
I have an Iframe in one of the pages i am developing... the iframe is suppose to scroll up and down and the links target the iframe. the problem is that the iframe scrolls side to side on internet...
3
by: matwilko | last post by:
hi, i am trying to create a simple version of itunes...and i am using iframes to do this. I have already set up the iframes using dreamweaver and used a drop-down menu to select the genre. When...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.