473,803 Members | 3,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem dynamically writing HTML

I am trying to learn to write code dynamcially using javascript and the
W3C DOM. Does anybody know why this code mihgt be giving me trouble?

document.getEle mentsByTagName( "head")[0].appendChild(do cument.createEl ement("title")) ;
alert(document. getElementsByTa gName("title")[0].tagName);
document.getEle mentsByTagName( "title")[0].appendChild(do cument.createTe xtNode("test")) ;

I get an error on the 3rd line: "Unexpected call to methor or property
access." I can't see why it's unexpected...

Dec 6 '05 #1
7 1290
VK

ezmiller wrote:
document.getEle mentsByTagName( "head")[0].appendChild(do cument.createEl ement("title")) ;
alert(document. getElementsByTa gName("title")[0].tagName);
document.getEle mentsByTagName( "title")[0].appendChild(do cument.createTe xtNode("test")) ;


Jis Almighty! :-O

document.title = "What I want";

// If you read DOM too much it makes damage - I have my proofs
// (not meaning you!) :-)

Dec 6 '05 #2
Ok, but this is an exercise. I'm trying to learn DOM. Theoretically, I
should be able to do it this way right? I'm hoping that if I can
figure out why this is not working, then I will understand the DOM
better.

Dec 6 '05 #3
>document.getEl ementsByTagName ("title")[0].appendChild(do cument.createTe xtNo*de("test") );

I get an error on the 3rd line: "Unexpected call to methor or property
access." I can't see why it's unexpected...


I don't know for sure offhand, so you may want to research this - but I
don't believe <title> elements have an appendChild method. Therefore,
you are calling a method for the element that doesn't exist.

Dec 6 '05 #4
VK

ezmiller wrote:
Ok, but this is an exercise. I'm trying to learn DOM. Theoretically, I
should be able to do it this way right? I'm hoping that if I can
figure out why this is not working, then I will understand the DOM
better.


OK, for better understanding you can also try:

1) create several table rows and add it to an existing table using
innerHTML

2) appendChild <div> to <p> or appendChild <p> to <div>

3) can give you more...

Somewhere it will work somewhere it doesn't. I guess W3C has some
answers at <http://www.w3.org> but I'm not going in there unless I'm
good paid or by brute force ;-).
My wild guess would be that TITLE (as the entire HEAD section) is not
considered to be included to the document text flow, so no textNode
allowed.

If you are learning (not hacking) DOM then on the primary stage you
should:

1) Do not go anywhere outside of document.body - that is your kingdom.

2) Always follow the natural relationship of elements (thus do not make
form to be a child of textfield).

3) Never append block elements (<p>, <div>) to inline elements (<span>,
<b>, <i>).

4) Remember that <table> is all special structure in comparison of
document.body so it is needed to be treated differently. W3C vs.
Microsoft is here:
<http://msdn.microsoft. com/workshop/author/tables/buildtables.asp >

5) Do not trust anyone (including myself) and any docs unless it indeed
works as explained ;-)

Dec 6 '05 #5
Now that seems like a plausible explanation, but the thing is that as I
understand it, every node should conform to the Node interface, and the
nodeInterface specifiies a appendChild function as well as a
removeChild function....

Dec 6 '05 #6
VK wrote:
ezmiller wrote:
Ok, but this is an exercise. I'm trying to learn DOM. Theoretically, I
should be able to do it this way right? I'm hoping that if I can
figure out why this is not working, then I will understand the DOM
better.
OK, for better understanding you can also try:
[...]
2) appendChild <div> to <p> or appendChild <p> to <div>


Where the former SHOULD fail. `p' elements MUST NOT contain `div' or
other block-level elements.

,-<URL:http://www.w3.org/TR/html4/sgml/dtd.html>
|
| <!ENTITY % fontstyle
| "TT | I | B | BIG | SMALL">
|
| <!ENTITY % phrase "EM | STRONG | DFN | CODE |
| SAMP | KBD | VAR | CITE | ABBR | ACRONYM" >
|
| <!ENTITY % special
| "A | IMG | OBJECT | BR | SCRIPT | MAP | Q | SUB | SUP | SPAN | BDO">
|
| <!ENTITY % formctrl "INPUT | SELECT | TEXTAREA | LABEL | BUTTON">
|
| <!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special;
| | %formctrl;">
|
| [...]
| <!ELEMENT P - O (%inline;)* -- paragraph -->

However, `div' elements may contain `p' elements:

| <!ENTITY % block
| "P | %heading; | %list; | %preformatted; | DL | DIV | NOSCRIPT |
| BLOCKQUOTE | FORM | HR | TABLE | FIELDSET | ADDRESS
|
| <!ENTITY % flow "%block; | %inline;">
|
| [...]
| <!ELEMENT DIV - - (%flow;)* -- generic language/style container -->
[...]
5) Do not trust anyone (including myself) and any docs unless it indeed
works as explained ;-)


Q.e.d.? ;-)
PointedEars
Dec 8 '05 #7
ezmiller wrote:
Now that seems like a plausible explanation, but the thing is that as I
understand it, every node should conform to the Node interface, and the
nodeInterface specifiies a appendChild function as well as a
removeChild function....


Just for clarification, the interface implementation tree we are talking
about is

Node (W3C DOM Level 2 Core)
|
'- Element (W3C DOM Level 2 Core)
|
'- HTMLElement (W3C DOM Level 2 HTML)
|
'- HTMLTitleElemen t (W3C DOM Level 2 HTML)

So you are right that HTMLTitleElemen t objects (i.e. objects implementing
that interface) should have an appendChild() method, and indeed they have
here (Firefox/1.0.7 on GNU/Linux).

However, that `title' element represented can have only and must have one
child text node, and that text node is already there, even if empty. Try

alert(document. getElementsByTa gName("title")[0].childNodes.len gth);

in a Valid HTML 4.01 document with <title></title>. Should yield 1, and
if you try

alert(document. getElementsByTa gName("title")[0].childNodes[0].nodeType);

it should yield 3 which equals the value of Node::TEXT_NODE .

Removing it would invalidate the underlying markup.

<URL:http://www.w3.org/TR/html4/struct/global.html#ede f-TITLE>

Note that the #PCDATA content may be empty, but is AIUI _not_ optional.

Therefore the specified shortcut HTMLDocument::t itle, implemented as
document.title, exists to read and modify the value of that text node.

JFTR: In my UA,

document.getEle mentsByTagName( "title")[0].nodeValue = "blurb";

does work (`nodeValue' value is changed, no error or exception), but does
not change the title of the window/tab. document.title= "blurb" changes the
title of the window/tab, but does not change the nodeValue. I assume this
peculiarity is due to history when document.title was already part of DOM
Level 0.
PointedEars
Dec 8 '05 #8

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

Similar topics

20
1857
by: Nick | last post by:
Right now I'm using document.write("<script language='javascript' src='jsFile" + i + ".js'></script>"); It works -- I have a lot of data in each file and only want the visitor to have to download the necessary file. Is there a better way to do this? Just curious because the page doesn't validate in HTML validators.
3
2272
by: Eric | last post by:
I have built a composite user web control that I want to create dynamically. The form will contain a variable number of these controls and as well some of the contents of the user web control itself are dynamically created controls. I create as follows: wcGroupControl oGroupControl = new wcGroupControl(); I then tried calling methods of the control passing dynamic controls as parameters that need to be added to the web controls on the...
2
1315
by: stb | last post by:
Hi. Is it possible to write inline Templates for a DataList dynamically in the HTML code, based on the DataSet that is bound to the DataList? Anyone know how?
2
7367
by: Axel Dahmen | last post by:
HI, I want to dynamically add controls to a web page from within a common base class. Unfortunately, ASP.NET fails with "System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)." All of my pages contain these kind of blocks, for several reasons. For one, I don't know any other way to dynamically add header information like e.g. style sheet links to a web page. So, is...
4
1824
by: Stu | last post by:
Hi, I am writing a content management system that has to have W3C compliant output. The pages are template driven and there are special strings within the template to be used as placeholders for the content. However, when I output the content to a literal (as shown below) the body and html tags are within the form tags. Is there any way to position the form tags within the dynamically generated text (ie to just after the opening body...
2
1600
by: Web Team | last post by:
Hi All, I'm in the process of writing a eich text editor web custom control. The actual text/HTML is displayed/editied in a DIV layer, which I have created like this: output.Write("<span class=" & EditDivCSSClass & " id=" & Me.ID & " " & strContentEditable & " wrap>" & _strText & "</span>")
2
2290
by: ezmiller | last post by:
Hi, I have some code (which I will paste in below) that writes out some HTML dynamically using the W3C DOM...the last part of the code write out a simple table. MY problem is that the table is not showing up, even though it seems to be part of the document (i.e. I can reach the nodes through through document.body.getElementsByTagName... ) var e; var f; // Will hold a document fragment.
2
1195
by: mamin | last post by:
Hi, I need to create html file dynamically from C# code. I need to place there some javascript code. This js code I'm keeping in fukctions.js file. Now, I'm generating HTML code, for example: string htmlContent ="<html><body></body></html>"; ...... saveHtmlFile(htmlContent);
1
1632
by: virajitha | last post by:
Hi .... I am facing a major problem in writing a javascript that will load an xml document dynamically and posts it to the next page when clicked on a hyperlink. The problem is that in the below code i am not able to create the fso object . I called five alert statements alternatley. when I load the html page i call this function hello in the body tag with onload event. when the page loads i get only 3 msg boxes and no message box appears...
1
7554
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and having the sql statement dynamically built according to the input provided by the user. I have used the method described here hundreds of times it is quick and adaptive. I generally use a frames page for the search, in this way the search is maintained...
0
9703
marktang
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...
0
9564
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,...
0
10548
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
10069
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...
1
7604
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
6842
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();...
0
5500
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...
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
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.