473,890 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DOM error: "node cannot be inserted". Why not?

I am trying to write an editor object, which adds some functionality
and a toolbar to every textarea with a "form-textarea" class. Both FF
and IE generate an error in line 20
(container.appe ndChild(this.to olbar);) saying "Node cannot be inserted
at the specified point in the hierarchy" code: "3"

I have two questions:
- who would help me with this error
- please provide other feedback about my code, since I want to write a
solid framework

I've pasted all code (including general library functions), to provide
a complete overview.

Any help is greatly appreciated,
Dennis
/*************** *************** **** editor code
*************** *************** */
var Editor = {
version : "1.0 beta",

init : function() {
var textareaNodes = document.getEle mentsByTagName( 'textarea');
var thisTextarea, i = 0;
while ((thisTextarea = textareaNodes[i++])) {
if (hasClass(thisT extarea, 'form-textarea')) {
new this.textEditor (thisTextarea);
}
}
},

textEditor : function(textar ea) {
this.textarea = textarea;
this.value = textarea.value. replace(/\s+$/g, "");
this.textarea.c lassName = 'editor-textarea';
this.textarea.r ows = this.value.spli t("\n").lengt h;
var container = createNode('div ', {
'class' : 'texteditor ' + this.textarea.i d}
);
this.toolbar = Editor.textTool bar;
container.appen dChild(this.too lbar);
container.appen dChild(this.tex tarea);
this.textarea.p arentNode.inser tBefore(contain er, this.textarea);
Editor.detectKe y(this.textarea );
this.textarea.f ocus();
},

detectKey : function(textar ea) {
var theHandler = this.handleKey;
if (document.all) textarea.onkeyd own = function(e) {
theHandler.call (self, e, textarea);
}
else textarea.onkeyp ress = function(e) {
theHandler.call (self, e, textarea);
}
},

handleKey : function(e, textarea) {
if (!e || e == null) var e = window.event;
if (e.keyCode) key = e.keyCode;
else if (e.which) key = e.which - 32;
Editor.growText area(textarea);
},

growTextarea : function(textar ea) {
var value = textarea.value, newlines = value.split("\n "), i = 0,
rows = 1;
while (newline = newlines[i++]) {
rows += Math.ceil(newli ne.length / textarea.cols);
}
textarea.rows = rows;
},

textToolbar : function() {
str = createNode('inp ut', {'type' : 'checkbox'});
return str;
}
}

addLoadEvent(fu nction () {
Editor.init();
});

/*************** *************** * library functions
*************** *************/

function addLoadEvent(fu nc) {
var oldOnload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
}
else {
window.onload = function() {
oldOnload();
func();
}
}
}

function hasClass(el, className) {
return (
el.className &&
el.className.ma tch(new RegExp("\\b" + className + "\\b"))
) ? true : false;
}

function createNode(type , options) {
var el = document.create Element(type);
for (var key in options) {
el.setAttribute (key, options[key]);
}
return el;
}
Jun 27 '08 #1
7 3366
On May 14, 10:52 am, dennis.spreng.. .@gmail.com wrote:
I am trying to write an editor object, which adds some functionality
and a toolbar to every textarea with a "form-textarea" class. Both FF
and IE generate an error in line 20
(container.appe ndChild(this.to olbar);) saying "Node cannot be inserted
at the specified point in the hierarchy" code: "3"

I have two questions:
- who would help me with this error
- please provide other feedback about my code, since I want to write a
solid framework

I've pasted all code (including general library functions), to provide
a complete overview.

Any help is greatly appreciated,
Dennis

/*************** *************** **** editor code
*************** *************** */
var Editor = {
version : "1.0 beta",

init : function() {
var textareaNodes = document.getEle mentsByTagName( 'textarea');
var thisTextarea, i = 0;
while ((thisTextarea = textareaNodes[i++])) {
I think it is better here to use:

var i = textareaNodes.l ength;
while (i--) {
thisTextarea = textareaNodes[i];
...
}
Usually I have found that puting assignments inside conditionals is
slower than doing them outside. Also, a plain for loop is probably
more readable for most and just as fast in modern browsers.

if (hasClass(thisT extarea, 'form-textarea')) {
new this.textEditor (thisTextarea);
}
}
},

textEditor : function(textar ea) {
this.textarea = textarea;
this.value = textarea.value. replace(/\s+$/g, "");
this.textarea.c lassName = 'editor-textarea';
this.textarea.r ows = this.value.spli t("\n").lengt h;
var container = createNode('div ', {
'class' : 'texteditor ' + this.textarea.i d}
);
this.toolbar = Editor.textTool bar;
Did you mean to call it?

this.toolbar = Editor.textTool bar();

container.appen dChild(this.too lbar);
Presumably you want to append whatever is returned by the call to
Editor.textTool bar, which is an input element.

container.appen dChild(this.tex tarea);
this.textarea.p arentNode.inser tBefore(contain er, this.textarea);
Here is your next problem - this.textarea.p arentNode is container (the
line above makes it so), so it is effectively:

container.inser tBefore(contain er, this.textarea)

The first argument to insertBefore should be the node to be inserted
(the new child), container can't be inserted into itself.

<URL: http://www.w3.org/TR/DOM-Level-2-Cor...l#ID-952280727 >

--
Rob
Jun 27 '08 #2
de************* *@gmail.com meinte:
I am trying to write an editor object, which adds some functionality
and a toolbar to every textarea with a "form-textarea" class. Both FF
and IE generate an error in line 20
(container.appe ndChild(this.to olbar);) saying "Node cannot be inserted
at the specified point in the hierarchy" code: "3"

I have two questions:
- who would help me with this error
- please provide other feedback about my code, since I want to write a
solid framework

I've pasted all code (including general library functions), to provide
a complete overview.

Any help is greatly appreciated,
Dennis

detectKey : function(textar ea) {
var theHandler = this.handleKey;
if (document.all) textarea.onkeyd own = function(e) {
theHandler.call (self, e, textarea);
}
else textarea.onkeyp ress = function(e) {
theHandler.call (self, e, textarea);
}
},
What's this document.all check supposed to do? Lets see: IE knows
document.all, Opera knows it, Safari does, Firefox doesn't (IIRC it does
in quirks mode - but since I've never needed and used document.all I
might be wrong). Anyway, why are you assigning the same listener to
different handlers then?
handleKey : function(e, textarea) {
if (!e || e == null) var e = window.event;
e = e || window.event; does the same.

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jun 27 '08 #3
On May 14, 4:04 pm, Gregor Kofler wrote:
dennis.spreng.. .@gmail.com meinte:
<snip>
What's this document.all check supposed to do? Lets see:
IE knows document.all, Opera knows it, Safari does,
Firefox doesn't (IIRC it does in quirks mode - but since
I've never needed and used document.all I might be wrong).
Anyway, why are you assigning the same listener to
different handlers then?
> handleKey : function(e, textarea) {
if (!e || e == null) var e = window.event;

e = e || window.event; does the same.
While you are at it, all values of - e - that could result in true for
- e == null - will result in true for - !e -, so whenever the - e ==
null - expression is evaluated the result must always be false, making
the expression redundant and its evaluation a needless runtime
overhead.

Declaring - var e - is pointless as the function's formal parameter -
e - results in the Variable object having an 'e' property by the time
the variable declaration is processed (so it cannot result in the
creation of such a property), and that variable declaration is not
allowed to alter the value of any pre-existing properties of the
Variable object. Making that declaration pointless at best and a
needless runtime overhead at worst.
Jun 27 '08 #4
Gregor Kofler wrote:
de************* *@gmail.com meinte:
> handleKey : function(e, textarea) {
if (!e || e == null) var e = window.event;

e = e || window.event; does the same.
More efficient, and recommended:

if (!e) e = window.event;
if (e)
{
// ...
}

The `=' operation performed always only adds needless runtime overhead[tm]
if e == true, and the value assigned, if necessary, should be tested for as
well.
PointedEars
--
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 #5
Thomas 'PointedEars' Lahn <Po*********@we b.dewrites:
Gregor Kofler wrote:
>e = e || window.event; does the same.

More efficient, and recommended:

if (!e) e = window.event;
I wouldn't recommend either over the other.

The minuscule difference in execution time is unlikely to be relevant,
since events are typically caused by user interaction, which dwarves
any such small optimizations.

/L 'Premature optimization is the root of all evil'
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 27 '08 #6
On May 15, 2:07 am, Henry <rcornf...@rain drop.co.ukwrote :
On May 14, 4:04 pm, Gregor Kofler wrote:
dennis.spreng.. .@gmail.com meinte:
<snip>
What's this document.all check supposed to do? Lets see:
IE knows document.all, Opera knows it, Safari does,
Firefox doesn't (IIRC it does in quirks mode - but since
I've never needed and used document.all I might be wrong).
Anyway, why are you assigning the same listener to
different handlers then?
handleKey : function(e, textarea) {
if (!e || e == null) var e = window.event;
[...]
Declaring - var e - is pointless as the function's formal parameter -
e - results in the Variable object having an 'e' property by the time
the variable declaration is processed (so it cannot result in the
creation of such a property), and that variable declaration is not
allowed to alter the value of any pre-existing properties of the
Variable object. Making that declaration pointless at best and a
needless runtime overhead at worst.
The above explanation is correct for browsers that conform to the
ECMAScript specification, however for old versions of Safari
(somewhere around 1.0 I think) the declaration with var is required in
this particular case due to a browser bug, though I seem to remember
using:

var e = e || window.event;

The declaration is performed before any code is executed, therefore it
should have zero effect on performance after its initial parsing.
--
Rob
Jun 27 '08 #7
RobG wrote:
On May 15, 2:07 am, Henry <rcornf...@rain drop.co.ukwrote :
>On May 14, 4:04 pm, Gregor Kofler wrote:
>>dennis.spreng ...@gmail.com meinte:
handleKey : function(e, textarea) {
if (!e || e == null) var e = window.event;
[...]
>Declaring - var e - is pointless as the function's formal parameter -
e - results in the Variable object having an 'e' property by the time
the variable declaration is processed (so it cannot result in the
creation of such a property), and that variable declaration is not
allowed to alter the value of any pre-existing properties of the
Variable object. Making that declaration pointless at best and a
needless runtime overhead at worst.

The above explanation is correct for browsers that conform to the
ECMAScript specification, however for old versions of Safari
(somewhere around 1.0 I think) the declaration with var is required in
this particular case due to a browser bug, [...]
I don't follow. Do you mean to say that Safari 1.x did not support named
arguments or did not allow to modify argument values? If so, I would simply
remove this UA from considerations because it is clearly FUBAR.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8************ *******@news.de mon.co.uk>
Jun 27 '08 #8

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

Similar topics

5
9552
by: George Copeland | last post by:
This is a request for help fixing a SQL Server 2000/ADO problem on Windows XP. I would appreciate any useful assistance. PROBLEM: SQL Server access on my machine fails as follows: 1. All of my VB6 apps reference the following ADO typelib: Microsoft ActiveX Data Objects 2.7 Library Located at: c:\Program Files\Common Files\System\ADO\msado27.tlb
1
3753
by: chris yoker | last post by:
hiya, I succesfully return a "nodeList" thru the "xmlDoc.SelectNodes" method. This nodeList is taken from a repetitive, uniform xml doc. I can successfully append a child node at the correct position using the following code: <code> <!--Node inserted into corrrect position in nodeList(0)-->
3
445
by: Tim Marsden | last post by:
Hi, When I open my VS2003 solution it looks up "Initializing offline cache", if I end it and reopen its OK. Any suggestions? Tim
8
4380
by: Douglas | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hello, The following code does not compile if line 3 is uncommented "using namespace std". I do not understand it. Could somebody explain it to me? I am using MSVC 6.0. Thanks a lot,
14
59046
by: W2K3R2admin | last post by:
I cannot open help and support...how do I install it on an existing system? When I click on Help & Support, the error says "Windows cannot open Help and Support because a system service is not running. To fix this problem start the service named "help and support." I have tried cmd prompt and 'msconfig' I have double checked by going to control panel/administrative tools/services Help and Support is not listed in either place so I...
1
1367
by: sanghamitra das | last post by:
We have developed a web application in which we are using login user(as a array structure) in session variable using HttpContext for some purpose. The session is availabe in the same project throughout. But, when a class in the dll of the project tries to look up the session object, ArgumentOutOfRangeException is raised. The problem is that this happens only once in a while after publishing the website. When the IIS is restarted, everything...
3
8431
by: masterofzen | last post by:
I've been playing around with this for a while. I bet the answer is pretty obvious, but I'm just not seeing it. I'm also reasonably certain I've run into this problem before. I'm trying to dynamically insert a few nodes into a webpage using insertBefore. Here's the HTML in question. (Yes, I know the HTML is terrible, but I didn't write it and can't edit it, which is why I have to insert things through the DOM instead. Long story.) <div...
8
6311
by: charli | last post by:
Error 3265, "Item cannot be found in the collection corresponding to the requested name or ordinal" code programatically opens a query using ADOX and changed the sql Dim cat As New ADOX.Catalog Dim cmd As New ADODB.Command cat.ActiveConnection = CurrentProject.Connection Set cmd = cat.Procedures("qryLostCostCodes").Command 'opens existing query to edit
2
6833
by: dowlingm815 | last post by:
I am attempting to create a table with an index. however, i am receiving an error code "3371: Cannot find table or constraint" as soon as it reads the "create index" statement. The code is as follows" thank you in advance for any guidance. strSeqNumber_TEMP = " " strIndexOrg_TEMP = "0" strFund_TEMP = " " strAccountCode_TEMP = "0"
0
9976
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
10802
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...
0
10451
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
8008
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
7160
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
5835
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...
1
4665
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4259
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3267
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.