473,796 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

innerHTML vs createElement

Given that I need to be able to add a TYPE attribute when I'm using
createElement and it seems to fail in both IE and FF (but not MZ) is it
'safer' to use innerHTML instead?

I can dynamically build the things I need to but I wondering if it will
introduce some new problems that won't surface till I get to testing.

Andrew Poulos
Jul 23 '05 #1
6 6856
Andrew Poulos wrote:
Given that I need to be able to add a TYPE attribute when I'm using
createElement and it seems to fail in both IE and FF (but not MZ)
Really? Then you have discovered a bug that no one else has.
Please post an example of your failing code. Below the signature
is a test case that happily creates inputs of whatever type you
want in both Firefox and IE (provided you enter a valid type to
create, of course).

is it 'safer' to use innerHTML instead?
No. But in some circumstances it is more convenient.
I can dynamically build the things I need to but I wondering if it will
introduce some new problems that won't surface till I get to testing.


Without code or a URL to comment on, it's pointless speculating
what might be wrong.

--
Rob.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<title>type</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/JavaScript">

// p is the element to add the input to
// t is the type of input to create
function addInput(p,t) {
var oT = document.create Element('INPUT' );
oT.type = t;
p.appendChild(o T);
}
</script>
</head><body>
<form action="">
<label for="typeField" >Enter an input type to append:
<input type="text" value="button" name="typeField "></label>
<br>
<input type="button" value="Add input" onclick="
addInput(this.f orm,this.form.t ypeField.value) ;
">
</form>
</body>
</html>
Jul 23 '05 #2
RobG wrote:
Andrew Poulos wrote:
Given that I need to be able to add a TYPE attribute when I'm using
createElement and it seems to fail in both IE and FF (but not MZ)


Really? Then you have discovered a bug that no one else has.
Please post an example of your failing code. Below the signature
is a test case that happily creates inputs of whatever type you
want in both Firefox and IE (provided you enter a valid type to
create, of course).


Not so. See Michael Winter's comment (number 10) here:
<url:http://groups.google.c om.au/groups?hl=en&lr =&threadm=41c82 4cb%240%2425815 %245a62ac22%40p er-qv1-newsreader-01.iinet.net.au &rnum=1&prev =/groups%3Fq%3D%2 522Michael%2BWi nter%2522%2B%25 2Btype%2B%252BA ndrew%2BPoulos% 2522%26hl%3Den% 26lr%3D%26selm% 3D41c824cb%2524 0%252425815%252 45a62ac22%2540p er-qv1-newsreader-01.iinet.net.au %26rnum%3D1>

Also if you check an earlier thread titled "Dynamic Object tag works in
MZ but not FF" you'll see some code that fails, when the video is a .AVI
file.
is it 'safer' to use innerHTML instead?


No. But in some circumstances it is more convenient.


I sort of understand but when using DOM 'fails' I don't really have an
alternative.
I can dynamically build the things I need to but I wondering if it
will introduce some new problems that won't surface till I get to
testing.


Without code or a URL to comment on, it's pointless speculating
what might be wrong.


Sorry, I was speaking generally. What I meant was, will innerHTML cause
'new' problems that may not surface until I test on other major OSs and
browsers? (My development machine is currently Win XP SP1 running IE 6
MZ 1.7.5 and FF 1.0. My other machine has Win 98SE and IE 5.)

Andrew Poulos
Jul 23 '05 #3
RobG wrote:
Andrew Poulos wrote:
Given that I need to be able to add a TYPE attribute when I'm using
createElement and it seems to fail in both IE and FF (but not MZ)


Just checked the HTML 4 spec, TYPE attributes are allowed on
the following elements:

a (link), object, param, script, style, input, li, ol, ul and
button.

<URL:http://www.w3.org/TR/html4/index/attributes.html >

My limited example is on <input> types, what element were you
having trouble with?

--
Rob.
Jul 23 '05 #4
RobG wrote:
RobG wrote:
Andrew Poulos wrote:
Given that I need to be able to add a TYPE attribute when I'm using
createElement and it seems to fail in both IE and FF (but not MZ)



Just checked the HTML 4 spec, TYPE attributes are allowed on
the following elements:

a (link), object, param, script, style, input, li, ol, ul and
button.

<URL:http://www.w3.org/TR/html4/index/attributes.html >

My limited example is on <input> types, what element were you
having trouble with?

Putting TYPE attributes on an OBJECT tag fails for me in FF.

Here's a sample that fails in FF but not MZ:

window.onload = function() {
addParam = function(p,n,v) { /* parent, name, value */
var c = document.create Element("param" );
c.name = n;
c.value = v;
p.appendChild(c );
}

// parameters
var vidUrl = "media/sample.avi";
var vidTop = 100;
var vidLeft = 150;
var vidWidth = 320;
var vidHeight = 240;
var vidCont = 17; /* add 17px for the QT controller */

// create a DIV to contain the QT OBJECT
var v = document.create Element("div");
v.setAttribute( "id", "videoContainer ");
v.style.positio n = "absolute";
v.style.left = "0px";
v.style.top = "0px";
document.body.a ppendChild(v);

// QT OBJECT for non IE
var m = document.create Element("object ");
m.data = vidUrl;
m.type = "video/quicktime";
m.width = vidWidth;
m.height = vidHeight + vidCont;
m.style.positio n = "absolute";
m.style.left = vidLeft + "px";
m.style.top = vidTop + "px";
addParam(m,"Aut oStart","false" );
addParam(m,"Con troller","true" );
v.appendChild(m );
}

- The DOM inspector shows that the TYPE attribute has not been added to
FF while it is in MZ.
- The AVI plays in MZ but FF tells me there's a missing plugin.
- If I write it into the HTML it works in FF
- If I use innerHTML it works in FF
- Moving the adding of the attributes to after the appending of the
OBJECT tag had no apparent effect, ie it still didn't work.

Andrew Poulos
Jul 23 '05 #5
Andrew Poulos wrote:
RobG wrote: [...] Not so. See Michael Winter's comment (number 10) here:
<url:http://groups.google.c om.au/groups?hl=en&lr =&threadm=41c82 4cb%240%2425815 %245a62ac22%40p er-qv1-newsreader-01.iinet.net.au &rnum=1&prev =/groups%3Fq%3D%2 522Michael%2BWi nter%2522%2B%25 2Btype%2B%252BA ndrew%2BPoulos% 2522%26hl%3Den% 26lr%3D%26selm% 3D41c824cb%2524 0%252425815%252 45a62ac22%2540p er-qv1-newsreader-01.iinet.net.au %26rnum%3D1>
Ah, so you are talking about adding type to an object tag.

Mike's words weren't a general comment that adding type
attributes won't work, but was (if I understand correctly)
specifically in regard to adding type to objects:

" 3) Ignore the type attribute. You will *not* be able to
set it. From what I found whilst playing, the type attribute
can only be set from code if the value is of a limited subset
of MIME types, and even then, the permitted types depend on
the other attributes of the element."
Also if you check an earlier thread titled "Dynamic Object tag works in
MZ but not FF" you'll see some code that fails, when the video is a .AVI
file.
Again, thanks for adding context.
is it 'safer' to use innerHTML instead?

No. But in some circumstances it is more convenient.

I sort of understand but when using DOM 'fails' I don't really have an
alternative.


In this specific case, perhaps you don't. If you must add type
and if innerHTML is the only way, do it. To remove doubt,
feature test and if it fails, deal with it.

if (...innerHTML) {
// insert type using innerHTML
} else {
// hope it works without type?
}

The issue with innerHTML is that it is not a W3C standard, it is
a method created by Microsoft and copied by other browsers.

There is a discussion of innerHTML vs DOM here:

<URL:http://www.developer-x.com/content/innerhtml/>

Though I can't vouch for the accuracy or correctness of any of
the contributors' opinions and they are a bit dated (April 2002).

[...] Sorry, I was speaking generally. What I meant was, will innerHTML cause
'new' problems that may not surface until I test on other major OSs and
browsers? (My development machine is currently Win XP SP1 running IE 6
MZ 1.7.5 and FF 1.0. My other machine has Win 98SE and IE 5.)


In general, if you keeps its use minimal, no. However, because
the "standard" is whatever Microsoft have implemented in IE,
there is no way to define what is the correct way for innerHTML
to do anything other than "this is how IE does it".

So the answer is: test, test, test... (but I think you knew
that!!) ;-)

--
Rob
Jul 23 '05 #6
On Thu, 06 Jan 2005 17:27:18 +1000, RobG <rg***@iinet.ne t.auau> wrote:

[snip]
Mike's words weren't a general comment that adding type
attributes won't work, but was (if I understand correctly)
specifically in regard to adding type to objects:


Correct. Furthermore, the implication was that the type attribute wasn't
needed - the user agent managed without it. Whether that still applies in
this case, I'm not sure. I'll leave that to the OP.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #7

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

Similar topics

1
6690
by: Sergey Batishchev | last post by:
I need to create element with some content inside. This may be any HTML stuff - tags, text. I do next Set newelem = doc.createElement("span") newelem.innerHTML = "<a href=""test\test.htm"">1st</a>" (code above is written in VB, but it does not matter) but in saved HTML I have anchor with absolute path <a href="file:///c:/test/test.htm">1st</a>
22
2866
by: necromonger | last post by:
Hi, I've got this code that creates a new new row and cell. I then put some text into the cell with innerHTML - works beautifully with Firefox but fails with IE. I guess IE doesn't support this way of doing it, but is there another way of doing it with DOM? newr = document.createElement('tr'); stbl.appendChild(newr);
1
1326
by: jhcorey | last post by:
I am doing some maintenance on a js tree control that was done with proprietary IE code. When I click on a node it calls a function which has code similar to that below to expand the tree. "el" refers to the div that I clicked on. var workHtml = el.innerHTML; for (i = 0; i < idx; i++)
4
6554
by: RobG | last post by:
I know you aren't supposed to use innerHTML to mess with table structure, but it seems you can't use it just after a table without damaging the containing element. I added a table to a div using createElement methods, then added a bit of extra text using innerHTML, only to find most of the attributes removed from the table. Below is a script that calls the same code to add a table inside a div. It adds an onclick to the div and...
16
13348
by: Joel Byrd | last post by:
I am having this ridiculous problem of trying to set the innerHTML of a div node. It works in all other browsers, but internet explorer is giving me an "Unknown runtime error". This is actually in the context of developing an auto-suggest (basically reverse-engineering Google Suggest), but I don't see how any of the other code has anything to do with it. I *pretty sure* that I've narrowed it down to 1 line (the line on which the...
10
12685
by: Jake Barnes | last post by:
This weekend I wanted to learn AJAX, so I set up a little toy page where I could experiment. The idea of this page is that you click in one of the boxes to get some controls, at which point you can add text, images, or HTML to the box. This seems to work fine in FireFox, but not in IE. You can see the problem here: http://www.publicdomainsoftware.org/ajaxExperiment.htm In FireFox, if you click in a box and then select "Add HTML" you...
9
8672
by: Hallvard B Furuseth | last post by:
Why does the FAQ (Q 4.15) recommend innerHTML when so many here say one should use createElement(), replaceChild() etc? Also, why does the "Alternative DynWrite function" at <http://www.jibbering.com/faq/faq_notes/alt_dynwrite.html> need such a lot of tests to find out if innerHTML assignment actually works, instead of just inserting <span id="strange name"></span> and checking if the document now contains an element with that ID?
3
1827
by: zero0x | last post by:
hi all, i'm trying to create something like window in javascript. there is no problem in firefox, but in microsoft internet explorer i get "Unknown error while working" (this message is in slovak language, and i has translated it, so maybe in english version it differs). when i comment this line: tab.innerHTML="test";
6
2316
by: SkyZhao | last post by:
if i use AttachEvent like this,it can't work; eg: var img = document.createElement("img"); img.attachEvent("onclick",alert("test")); var div = document.createElement("div"); div.appendChild(img); //can't work; div.innerHTML="<-click this";
0
10453
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10223
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10172
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.