473,713 Members | 2,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Differences between perhaps-similar constructs?

Inside of <script></script>, inside of <body></body>, what
are the differences between

document.body.c lassName="mysty le";
document.body.s etAttribute("cl ass", "mystyle");

? (...assuming that a CSS "mystyle" has been defined.)

=============== =============== =============== =============== ====
What are the differences between

word = new String("td");
word = createTextNode( "td");
Jun 27 '08 #1
3 1287
ge********@gmai l.com wrote:
Inside of <script></script>, inside of <body></body>, what
are the differences between

document.body.c lassName="mysty le";
document.body.s etAttribute("cl ass", "mystyle");

? (...assuming that a CSS "mystyle" has been defined.)
The first one is supposed to work, due to buggy implementations the second
one is not always supposed to work. The first one is therefore recommended.
=============== =============== =============== =============== ====
What are the differences between

word = new String("td");
word = createTextNode( "td");
The first one creates a new String object with the value "td" and returns a
reference to it that is then stored in `word'. Usually such is overkill as
ECMAScript implementations define the primitive string type:

word = "td";

The second one, as it is, likely causes a ReferenceError exception as no
object in the scope chain has such a method; if you meant

word = document.create TextNode("td");

instead, if supported it creates a new object implementing the TextNode
interface, with nodeValue == "td", and returns a reference to it that is
then stored in `word'. `word' referring to such an object can be used to
add a text node to the DOM tree; `word' referring to a String object or
`word' storing a string value cannot.
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jun 27 '08 #2

The question you actually ought to ask is, what are the differences

new String("td")
document.create TextNode("td")

I defer to PointedEars's answers for your questions about className
and createTextNode. However, the other forms are more nuanced. When
use the "new" keyword before a type like "String", or "Number", it
a boxed object which is distinct from a string or number literal.
are some invariants:

typeof "" == "string"

typeof String("") == "string"

typeof new String("") == "object"

These are all equivalent:
new String(x).toStr ing()
"" + x

And these are all equivalent:
new Number(n).value Of()

I personally avoid using "new".
Jun 27 '08 #3
On May 23, 8:33 pm, gentsqu...@gmai l.com wrote:
Inside of <script></script>, inside of <body></body>, what
are the differences between

document.body.c lassName="mysty le";
document.body.s etAttribute("cl ass", "mystyle");

? (...assuming that a CSS "mystyle" has been defined.)
First of all, it is not important _where_ the script is located: it is
only important _when_ it is called. In the particular
document.body.s omething=anythi ng can be only executed after DOM Tree
being ready thus after window's load event fired. Before that point
document.body is null so such expression will result in runtime error.
This way the proper question is:

After document being successfully loaded and window's load event
fired, is there any difference between i) scripting DOM interfaces and
ii) direct element node manipulation, other words are
document.body.c lassName="mysty le";
document.body.s etAttribute("cl ass", "mystyle");
equal by their functionality or not?

The answer is "yes and no". Yes, the resulting effect is often _but
not always_ the same. No, these are two technically completely
different processes where the first (scripting DOM interfaces) has to
be always used in Javascript and the second (direct element node
manipulation) should be used only in a narrow set of particular

The scripting DOM interfaces does exactly what the name implies and
what programmer wants: it gets/sets scriptable element properties to
affects its runtime look and/or behavior. Say
document.body.c lassName="mysty le";
means "apply to body style rules defined in mystyle class"
It also reflects the changes in the DOM Tree, so the body's node gets
extra attribute node "class" with value "mystyle". You can check that
by querying document.body.h asAttribute("cl ass") afterwards - except
for IE where hasAttribute is not functional until IE8 beta. But this
additional DOM Tree update happens only for properties defined in the
element's scriptable DOM interface. Say
document.body.f oobar = "foobar";
adds extra property to the DOM interface which you can use later - but
it is not reflected in the DOM Tree,
document.body.h asAttribute("fo obar") == false

From the other side getAttribute, setAttribute and hasAttribute
methods are intended to manipulate the element's node in the DOM Tree.
In the particular
document.body.s etAttribute("cl ass", "mystyle");
means "create attribute node "class" and set the text node value to
"mystyle". In many circumstances it still leads to DOM interface
requests afterwards so say document.body gets the style rules of
mystyle class applied. This behavior is of the same kind as say
window.location in IE allowed to be used both as a field and a method.
Other words sometimes it is easier to break the model rather than
trying to fight with legions of developers stubbornly making the same
mistake over and over again.

So to sum up the answer: always use the DOM scripting which is correct
- or use the setAttribute sidewalk which is silly but mostly

In either case there are two situations where only one of ways is

1) For each DOM interface there is a predefined set of properties and
methods it has by default. The attribute nodes with names not listed
in the set are not reflected in the DOM interface, at least not in all
browsers. Say having an element like
<input type="text"
name="email" id="email"
filltype="manda tory">
in Firefox will create an element node with attribute nodes "type",
"name", "id", "filltype". At the same time DOM interface will reflect
only three of them ("type", "name", "id") so
will be undefined because "filltype" is not in the default list. To
get the values of custom attribute one has then to use getAttribute
to query the DOM Tree:
document.forms['myform'].email.getAttri bute('filltype' )
will return "mandatory"

2) Intrinsic event handlers have to be set _only_ over the DOM
document.forms['myform'].onclick = myFunction;

email.setAttrib ute('onclick', 'alert(this.nam e)');
Now you know the above means "create "onclick" attribute node and set
its text node value to "alert(this.nam e)". By knowing it you will not
expect anymore that setting some text node value would have anything
to do with element event handlers. And this is a correct conclusion
because say IE does exactly what it means and nothing more: it creates
the mode with text value "alert(this.nam e)" but no event handler for
the input element. Event handlers are for DOM interface, not for DOM
=============== =============== =============== =============== ====
What are the differences between

word = new String("td");
word = document.create TextNode("td");
The first creates Javascript String object with valueOf "td"
The second creates DOM Tree text node with value "td".

The actual difference depends on how and where either is being used.

Jun 27 '08 #4

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

Similar topics

by: Dan Gass | last post by:
The difflib.py module and the diff.py tools script in Python 2.4 alpha 3 now support generating side by side (with intra line differences) in HTML format. I have found this useful for performing build release comparisons (I use a script to generate a main page showing all the files that were changed in a build with hyperlinks to side by side differences for each of those files). I also find it useful for generating HTML test reports to...
by: Daniel | last post by:
Hi, Are there any differences between C# and VB.Net besides syntax? Performance-wise, how do they compare? Thanks, Dan
by: Bern | last post by:
what are all the diferences between the two?
by: Kieran | last post by:
I am designing a content management system and I want to make sure all pages entered into it's database by users are using valid HTML. I have designed the system to use HTML 4.01 Transitional throughout, however the php powered html checker I have only caters for HTML 4.01 Strict. If possible I need to know the complete differences between Strict and Transitional so I can go through the php script and correctly set it up to properly...
by: hokieghal99 | last post by:
What is the difference between these two statements? Are there any major differences? const char filename = "ips_c.txt"; char filename = "ips_c.txt"; Thanks!!!
by: Blue | last post by:
Hi , Can any one please let me explain me the diffrences between "open"/ "fopen" or "read"/"fread" or "write/fwrite". I know that "open" /"read" / "write" are system calls and "fopen" /"fread" / "fwrite" are normal library functions. May be internally must be calling "fopen" /"fread" / "fwrite" functions.
by: Mark Cubitt | last post by:
the reason I ask is I have always used postgres at home and work, but my new web host only has mysql :( I want to know what sort of differences I will have in regards to programming php/perl webpages using the database. I current use sequences in postgres and understand this will be different in mysql, so can you tell me what the difference will be and if there are any other differences I should look out for.
by: Jeroen | last post by:
We have update operations on strings, where we know the "New" and "Old" version of the string. A html report will be generated about many of these changes. I would like to mark these changes with colors. Are there any .NET classes already available for this kind of string comparison? We're working with studio 2003.
by: drhowarddrfine | last post by:
I have a program that runs fine on FreeBSD but yesterday I transferred it over to a linux box and it failed. (Actually it's a web server on a shared host). What is happening is a run time linker utility is in /libexec but that path does not exist in Linux and, perhaps, not even that utility, don't know. The utility is ld-elf.so.1. It tries to call in some run-time libraries, I guess. I tried to compile it with gcc -static but it still...
by: Karol R | last post by:
Can anyone please explain me main differences between relational DB and warehouse (Point me to web site) ? Apart from theoretical differences I would like to know how Warehouse DB is updated ? If data needs to remain unchanged then what is happening for instance if in source relational DB address for a customer will change and record gets the update. How this is loaded to DB ?
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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...
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,...
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...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.