473,397 Members | 2,084 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,397 software developers and data experts.

appendChild not working in IE6

I have the following code:

##############
var nHead=(document.getElementsByTagName)?document.get ElementsByTagName("head").item(0):document.head;
var nStyle=document.createElement("style");
// nStyle.type="text/css";
nStyle.setAttribute("type","text/css");
nHead.appendChild(nStyle);
var cssText=document.createTextNode(
'\r\t.class1 { font-family: Verdana, sans-serif; font-size:1; color:#ffff00; font-weight: bold;} '+
'\r\t.class2 { font-family: Verdana, sans-serif; font-size:1; color:#00ffff; font-weight: bold;} '+
'\r\t.class3 { font-family:Arial,sans-serif;font-size:3;color:#00ff00;font-weight: bold;} '+
'\r\t.class4 { font-family:Arial,sans-serif;font-size:3;color:#0000ff;font-weight: bold;} '+
'\r\t.class5 { font-family:Arial,sans-serif;font-size:3;color:#ff0000;font-weight: bold;} \r'
);

nStyle.appendChild(cssText);
##############

this last nStyle.appendChild(cssText); returns the following error in IE6:
Error: Unexpected call to method or property access.
Code: 0

in FF this seems to work

Can anybody point out the problem?
Jul 24 '05 #1
3 8728
Robi wrote:
I have the following code:

##############
var nHead=(document.getElementsByTagName)?document.get ElementsByTagName("head").item(0):document.head;
var nStyle=document.createElement("style");
// nStyle.type="text/css";
nStyle.setAttribute("type","text/css");
nHead.appendChild(nStyle);
Try this:

nstyle.cssText =
'\r\t.class1 { font-family: Verdana, sans-serif; font-size:1;
color:#ffff00; font-weight: bold;} '+
'\r\t.class2 { font-family: Verdana, sans-serif; font-size:1;
color:#00ffff; font-weight: bold;} '+
'\r\t.class3 {
font-family:Arial,sans-serif;font-size:3;color:#00ff00;font-weight:
bold;} '+
'\r\t.class4 {
font-family:Arial,sans-serif;font-size:3;color:#0000ff;font-weight:
bold;} '+
'\r\t.class5 {
font-family:Arial,sans-serif;font-size:3;color:#ff0000;font-weight:
bold;} \r';

##############
this last nStyle.appendChild(cssText); returns the following error in IE6:
Error: Unexpected call to method or property access.
Code: 0

in FF this seems to work

Can anybody point out the problem?


Not sure about cross-compatibility of cssText, or applicable standards.
Others here are more qualified to speak to those issues.

Jul 24 '05 #2


Robi wrote:

var nStyle=document.createElement("style");
// nStyle.type="text/css";
nStyle.setAttribute("type","text/css");
nHead.appendChild(nStyle);
var cssText=document.createTextNode(
'\r\t.class1 { font-family: Verdana, sans-serif; font-size:1; color:#ffff00; font-weight: bold;} '+
'\r\t.class2 { font-family: Verdana, sans-serif; font-size:1; color:#00ffff; font-weight: bold;} '+
'\r\t.class3 { font-family:Arial,sans-serif;font-size:3;color:#00ff00;font-weight: bold;} '+
'\r\t.class4 { font-family:Arial,sans-serif;font-size:3;color:#0000ff;font-weight: bold;} '+
'\r\t.class5 { font-family:Arial,sans-serif;font-size:3;color:#ff0000;font-weight: bold;} \r'
);

nStyle.appendChild(cssText);

this last nStyle.appendChild(cssText); returns the following error in IE6:
Error: Unexpected call to method or property access.
Code: 0


Known problem with IE, IE 4 started with its own object model and while
IE 5 and later are supposed to implement the W3C DOM unfortunately there
are some elements on which full core DOM support is not there so that
for instance appendChild does not work, script and style elements are
examples of that. It is mostly elements where IE already has/had its own
specialized API to construct the contents of the element, for instance
the contents of the style element is a (CSS) stylesheet and IE has its
own API to add rules to such a sheet.
So some way to add a style rule with IE/Win and with Mozilla and with
Opera 8 is

var style = document.createElement('style');
style.type = 'text/css';
var selector = 'html';
var properties = 'color: darkblue; background-color: lightblue';
document.getElementsByTagName('head').item(0).appe ndChild(style);
if (document.styleSheets) {
var lastSheet = document.styleSheets[document.styleSheets.length - 1];
if (lastSheet && typeof lastSheet.addRule != 'undefined') {
lastSheet.addRule(selector, properties);
}
else {
style.appendChild(document.createTextNode(selector + ' { ' +
properties + ' }'));
}
}
else {
style.appendChild(document.createTextNode(selector + ' { ' +
properties + ' }'));
}

Or you could use try/catch to catch the IE error and then script the IE way.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 24 '05 #3
ASM
Robi wrote:
I have the following code:

##############
var nHead=(document.getElementsByTagName)?document.get ElementsByTagName("head").item(0):document.head;
var nStyle=document.createElement("style");
// nStyle.type="text/css";
nStyle.setAttribute("type","text/css");
personaly, my IE (5.1 5.2 Mac)
is angry with 'type' in wahtever way I try to pass it

nStyle.setAttribute("type","text/css");
MyObj.type = "text/css"

have to innerHTML the complete tag ?

nHead.appendChild(nStyle);
var cssText=document.createTextNode(
'\r\t.class1 { font-family: Verdana, sans-serif; font-size:1; color:#ffff00; font-weight: bold;} '+
'\r\t.class2 { font-family: Verdana, sans-serif; font-size:1; color:#00ffff; font-weight: bold;} '+
'\r\t.class3 { font-family:Arial,sans-serif;font-size:3;color:#00ff00;font-weight: bold;} '+
'\r\t.class4 { font-family:Arial,sans-serif;font-size:3;color:#0000ff;font-weight: bold;} '+
'\r\t.class5 { font-family:Arial,sans-serif;font-size:3;color:#ff0000;font-weight: bold;} \r'
);

nStyle.appendChild(cssText);
##############

this last nStyle.appendChild(cssText); returns the following error in IE6:
Error: Unexpected call to method or property access.
Code: 0

in FF this seems to work

Can anybody point out the problem?

--
Stephane Moriaux et son [moins] vieux Mac
Jul 24 '05 #4

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

Similar topics

25
by: kie | last post by:
hello, i have a table that creates and deletes rows dynamically using createElement, appendChild, removeChild. when i have added the required amount of rows and input my data, i would like to...
2
by: samuel.adam | last post by:
Hi all, I am coding an AJAX DHTML whatever application and I was fed up with always typing a lot of appendChild() functions. I created a custom one called append_children() and wanted to share...
2
by: Stewart | last post by:
Originally posted in comp.lang.javascript: Newsgroups: comp.lang.javascript From: "Stewart" Date: 23 Aug 2005 02:50:04 -0700 Local: Tues, Aug 23 2005 10:50 am Subject: FireFox, RemoveChild,...
3
by: ezmiller | last post by:
So I have some code that gets the body element of another frame and then tries to dynamically write a table. The code fails when, after creating the table, I try to append it to the document. I...
4
by: Olorin | last post by:
Hello everyone. I'm playing around with some AJAX-ish stuff and encountered some problem in the JS side of the universe. Maybe someone here can suggest an alternative that works. I have...
4
by: joecap5 | last post by:
I have a main window and a menu window. The menu window is opened from the main window by clicking on "Open Menu". A google window can also be opened from the main window by clicking "Open Google"....
13
Atli
by: Atli | last post by:
Hi everybody. I'm making a Javascript system to build tables, so I can view large tables in smaller pieces. I use the "document.createElement" and "appendChild" methods to create and remove...
2
by: vsanjit | last post by:
Hi all, I've been trying to create a table dynamically upon the generation of en event using the appendChild method in Javascript. This seems to work fine in Firefox, but not in IE7. There's also...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
0
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...

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.