473,387 Members | 1,512 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,387 software developers and data experts.

Stylesheet problem with generated nodes

I'm having trouble applying a stylesheet to content I'm generating
after the fact.

Here's the sample code:

<html>
<head>
<title>CSS/DOM Problem Example</title>
<style type="text/css">
..historylinks {
a:link { color: blue; text-decoration: none }
a:active { color: red; text-decoration: none }
a:visited { color: blue; text-decoration: none }
a:hover { color: green; text-decoration: underline }
}
</style>
<script type="text/javascript">
function writeToHistory () {
var hlist = document.getElementById("historyList");
var li = document.createElement('li');
var a = document.createElement('a');
a.appendChild(document.createTextNode('Test!'));
a.setAttribute("onClick", "alert(\"Alert!\");");
a.setAttribute("class", "historylinks");
li.appendChild(a);
hlist.insertBefore(li,hlist.firstChild);
}
</script>
</head>
<body>
<p>When you click this button, a new link appears at the top of the
history list.
Click on one of these links to perform an action.</p>
<button onClick="writeToHistory();">Test</button>
<ul id="historyList">
<li>End of History</li>
</ul>
</body>
</html>

Mozilla reports that it doesn't understand the "a" declaration, and
that might be a part of it, but as far as I can tell it's valid CSS. I
tried assigning the style with both a.className and a.setAttribute;
both generate valid HTML, but neither takes.

(After /that/ I need to figure out why IE6 won't popup the Alert
box...)

I appreciate any help you offer.

Dec 15 '06 #1
11 2385
je*****@gmail.com wrote:
<style type="text/css">
.historylinks {
a:link { color: blue; text-decoration: none }
a:active { color: red; text-decoration: none }
a:visited { color: blue; text-decoration: none }
a:hover { color: green; text-decoration: underline }
}
You can't nest CSS that way, you need e.g.
.historylinks a:link { ... }
.historylinks a:active { ... }
.historylinks a:visited { ... }
.historylinks a:hover { ... }
var a = document.createElement('a');
a.appendChild(document.createTextNode('Test!'));
a.setAttribute("onClick", "alert(\"Alert!\");");
Don't use setAttribute when scripting HTML documents, IE's
implementation is much different from what the W3C DOM specifies and
other browsers like Mozilla implement. Use element properties e.g.

a.onclick = function () { alert("Alert!"); };

and e.g.
a.setAttribute("class", "historylinks");
a.className = "historylinks";

that works across user agents when scripting HTML.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 15 '06 #2
VK

je*****@gmail.com wrote:
a.setAttribute("onClick", "alert(\"Alert!\");");
a.setAttribute("class", "historylinks");
li.appendChild(a);
hlist.insertBefore(li,hlist.firstChild);
<html>
<head>
<title>Demo</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function init() {
var lnk = document.links[0];
lnk.setAttribute('onclick', 'abrakadabra');
window.alert(lnk.getAttribute('onclick'));
// displays "abrakadabra"
}
window.onload = init;
</script>
</head>
<body>
<h1><a href="/">Demo</a></h1>
</body>
</html>

How strange, setAttribute / getAttribute seem working just fine
everywhere, including IE...

Oh, got it! You meant to say: "Setting onclick attribute value in the
relevant DOM Tree node is not always equal to setting onclick event
handler in the element's scriptable DOM Interface".

The question is then: why in the name would it be otherwise? It is true
that some browsers do parse attrubute values and do update the
interface thus they are making
element.setAttribute('onclick', 'strValue');
and
element.onclick = myFunction;
equal up to some extend (only "up to some extend" because DOM Tree and
DOM Interface are too different to completely hide it).

Such convenience "bridging" is neither directly prohibited nor required
by the current specs. It means that there can be always UA's without
such extension. One of such UA happens to take 85% or more of the
current market share: thusly it is highly recommended for now do not
use behavioral extension of setAttribute: use the standard interface
scripting methods instead:

a.onclick = myFunction;
a.className = 'historylinks';

Dec 15 '06 #3
Martin Honnen wrote:
Don't use setAttribute when scripting HTML documents, IE's
implementation is much different from what the W3C DOM specifies and
other browsers like Mozilla implement. Use element properties e.g.

a.onclick = function () { alert("Alert!"); };

and e.g.
> a.setAttribute("class", "historylinks");

a.className = "historylinks";

that works across user agents when scripting HTML.
Not altogether sound advice. IE /correctly/ refuses to set element
properties that are described as "read-only" in the DOM, but does allow
them to be set by setAttribute(). Firefox /incorrectly/ accepts the
property setting.

E.g.,

var post = document.createElement("input");
post.type = "submit";

will incorrectly work under Firefox, but correctly fail under IE, whereas

var post = document.createElement("input");
post.setAttribute("type", "submit");

will work everywhere.

--
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
Dec 15 '06 #4
VK said the following on 12/15/2006 2:16 PM:
<snip>

Is there some reason you would put a request in the subject yet you have
absolutely nothing in your post regarding any possible wording for an
entry? If you want to request an entry, by all means do so. But don't
imply in the subject that you want one and then not even mention it in
the post.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 16 '06 #5
John W. Kennedy wrote:
IE /correctly/ refuses to set element
properties that are described as "read-only" in the DOM, but does allow
them to be set by setAttribute(). Firefox /incorrectly/ accepts the
property setting.

E.g.,

var post = document.createElement("input");
post.type = "submit";
The current W3C DOM specification is W3C DOM Level 2 HTML
<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-62883744>
it does not say that the type property (respectively type attribute in
IDL terms) of HTMLInputElement objects is readonly. So using that sample
above to claim a DOM implementation is incorrectly accepting changes to
readonly properties is rubbish, that property is not readonly at all.
will incorrectly work under Firefox, but correctly fail under IE,
Both IE/Win (tested with IE 6) and Mozilla/Firefox allow the above code
without errors, that is creating a new input element object and setting
its type property directly after that before the input element object is
inserted into the document:

<http://home.arcor.de/martin.honnen/javascript/2006/12/test2006121601.html>
whereas

var post = document.createElement("input");
post.setAttribute("type", "submit");

will work everywhere.
See above test case, it does not make a difference in IE or Mozilla
whether you use inputElement.type (or input['type']) or
inputElement.setAttribute('type', '...'). The problem with IE's HTML DOM
and setAttribute is that it pays attention to the case of attribute
names and that it wants typed property values and not attribute strings:
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods/setattribute.asp>
There it clearly says that the second argument is not (always) a string
but rather a variant that can be a boolean, string or number. Then it
has a third argument to indicate whether case should matter or not, by
default it matters. In the W3C Core DOM the setAttribute method has two
attributes that are always strings, and there is no third attribute.
Where IE 6 refuses to set the type (but it refuses it with setAttribute
as well as setting the type property) is when the input is already in
the document:
<http://home.arcor.de/martin.honnen/javascript/2006/12/test2006121602.html>
So I think my advice is sound, in general and in particular for the
example properties the original poster had, onclick and className.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 16 '06 #6
ASM
Martin Honnen a écrit :
>
See above test case, it does not make a difference in IE or Mozilla
whether you use inputElement.type (or input['type']) or
inputElement.setAttribute('type', '...').
Yes that does any difference with my Firefox, but ...
The problem with IE's HTML DOM
and setAttribute is that it pays attention to the case of attribute
names and that it wants typed property values and not attribute strings:
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods/setattribute.asp>
my IE (Mac) doesn't accept to set type attribute what you'll can try
(the function breaks/stop from this point)
So I think my advice is sound, in general and in particular for the
example properties the original poster had, onclick and className.
nothing previously declared
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 16 '06 #7
ASM said the following on 12/16/2006 11:20 AM:
Martin Honnen a écrit :
>>
See above test case, it does not make a difference in IE or Mozilla
whether you use inputElement.type (or input['type']) or
inputElement.setAttribute('type', '...').

Yes that does any difference with my Firefox, but ...
>The problem with IE's HTML DOM and setAttribute is that it pays
attention to the case of attribute names and that it wants typed
property values and not attribute strings:
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods/setattribute.asp>


my IE (Mac) doesn't accept to set type attribute what you'll can try
(the function breaks/stop from this point)
IE on a Mac wouldn't be my choice of a browser to choose to use as a
demo of whether something works or not.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 16 '06 #8
Randy Webb wrote:
ASM said the following on 12/16/2006 11:20 AM:
<snip>
>my IE (Mac) doesn't accept to set type attribute what you'll
can try (the function breaks/stop from this point)

IE on a Mac wouldn't be my choice of a browser to choose to
use as a demo of whether something works or not.
I don't know, Mac IE might be a very good choice for demonstrating
something not working ;-)

Richard.
Dec 16 '06 #9
ASM
Richard Cornford a écrit :
Randy Webb wrote:
>ASM said the following on 12/16/2006 11:20 AM:
<snip>
>>my IE (Mac) doesn't accept to set type attribute what you'll
can try (the function breaks/stop from this point)
IE on a Mac wouldn't be my choice of a browser to choose to
use as a demo of whether something works or not.

I don't know, Mac IE might be a very good choice for demonstrating
something not working ;-)
some things work better than with IE 6

and ... you do what you want about info I can give ... :-(

Now I can also talk about my NC4 :-)
(I doesn't more see functions with gEBI taking care if it can be used)

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 16 '06 #10
Martin Honnen wrote:
John W. Kennedy wrote:
>IE /correctly/ refuses to set element properties that are described as
"read-only" in the DOM, but does allow them to be set by
setAttribute(). Firefox /incorrectly/ accepts the property setting.

E.g.,

var post = document.createElement("input");
post.type = "submit";

The current W3C DOM specification is W3C DOM Level 2 HTML
<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-62883744>
Dated late in 2000. IE6 (2001), as far as I can tell, implemented the
DOM 1 specs, where HTMLInputElement.type _is_ readonly.
it does not say that the type property (respectively type attribute in
IDL terms) of HTMLInputElement objects is readonly. So using that sample
above to claim a DOM implementation is incorrectly accepting changes to
readonly properties is rubbish, that property is not readonly at all.
>will incorrectly work under Firefox, but correctly fail under IE,

Both IE/Win (tested with IE 6) and Mozilla/Firefox allow the above code
without errors, that is creating a new input element object and setting
its type property directly after that before the input element object is
inserted into the document:
Then that's a recent patch, because it was failing on an up-to-date IE6
as recently as October (it cost me several hours of misery, IE6
diagnostic facilities being as vile as they are).
<http://home.arcor.de/martin.honnen/javascript/2006/12/test2006121601.html>
>whereas

var post = document.createElement("input");
post.setAttribute("type", "submit");

will work everywhere.

See above test case, it does not make a difference in IE or Mozilla
whether you use inputElement.type (or input['type']) or
inputElement.setAttribute('type', '...'). The problem with IE's HTML DOM
and setAttribute is that it pays attention to the case of attribute
names and that it wants typed property values and not attribute strings:
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods/setattribute.asp>

There it clearly says that the second argument is not (always) a string
but rather a variant that can be a boolean, string or number. Then it
has a third argument to indicate whether case should matter or not, by
default it matters. In the W3C Core DOM the setAttribute method has two
attributes that are always strings, and there is no third attribute.
Hey, I never said IE doesn't suck.
Where IE 6 refuses to set the type (but it refuses it with setAttribute
as well as setting the type property) is when the input is already in
the document:
<http://home.arcor.de/martin.honnen/javascript/2006/12/test2006121602.html>
So I think my advice is sound, in general and in particular for the
example properties the original poster had, onclick and className.
Let's just say the whole situation is a mess, no matter what approach
you take.

--
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
Dec 16 '06 #11
Richard Cornford said the following on 12/16/2006 12:26 PM:
Randy Webb wrote:
>ASM said the following on 12/16/2006 11:20 AM:
<snip>
>>my IE (Mac) doesn't accept to set type attribute what you'll
can try (the function breaks/stop from this point)
IE on a Mac wouldn't be my choice of a browser to choose to
use as a demo of whether something works or not.

I don't know, Mac IE might be a very good choice for demonstrating
something not working ;-)
You have a very valid point.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 16 '06 #12

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

Similar topics

1
by: Steven An | last post by:
I'm gonna describe the situation I'm dealing with..but if you're not interested, just skip to the last line where I bluntly ask my question :) Here's my scenario: I've got a huge website that...
3
by: Alvaro G Vicario | last post by:
I'm writing a small script to scan an HTML document and append a <span class="foo">Bar</span> node after each item with certain class name. My script works fine in Gecko but IE has a serious...
2
by: Richard L Rosenheim | last post by:
I loaded a XSLT stylesheet into a XMLDocument to retrieve some of the data. I received an exception when the SelectNodes method was invoked. The message was "System.Xml.XPath.XPathException -...
2
by: Mark | last post by:
I got this code from the Internet. Demo on the site works fine but downloaded version is not. I am a very new to .NET, I cannot figure this out, please help. Here is the code: ASPX code: <%@...
4
by: pbreah | last post by:
I'm doing a Rich Text Editor (WYSIWYG) in javascript for a game for kids. I'm doing a special case in with every keystroke from A-Z creates a background and foreground color for that letter, witch...
1
by: jesdynf | last post by:
I'm having trouble applying a stylesheet to content I'm generating after the fact. Here's the sample code: <html> <head> <title>CSS/DOM Problem Example</title> <style type="text/css">...
1
by: arunsh | last post by:
Hi I want an equivalent of what the getChildNodes() method does in xsl style sheet. (Note that the getChildNodes() method returns an array of nodes u nder the current node; ) My xml is...
2
by: milecimm | last post by:
Hello, I need some help to solve the following problem (if it is possible, that's it): I'm using a xpath expression to programatically get data from my xml file. I want to transform ONLY the...
6
by: _Who | last post by:
I use the code below to change to a style sheet that has: body { ....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: 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:
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...

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.