helmut
-
Posts
2 -
Joined
-
Last visited
Content Type
Profiles
Forums
Events
Posts posted by helmut
-
-
Hi, I intend to add a custom javascript object to the DOM of a HTML page to be able to access this object via its id (document.getElementById). Adding Nodes like Div..., that are known by DOM, works but where and how can I add the custom JScript Object. This is my testcode
<!DOCTYPE html><html lang="en"> <head> <meta charset="utf-8"> <script type="text/javascript"> function initJScript(){ var customObject = new customObjectConstructor(); var testDiv = document.createElement('div'); alert(testDiv); alert(customObject); alert(document); document.body.appendChild(testDiv); //Works document.appendChild(customObject); //Does NOT Work!!! How can i append a custom object [object Object] to DOM??? document.getElementById("customObjectId"); //to make this work } function customObjectConstructor(){ this.id = "customObjectId" this.x = 12345; } </script> </head> <body onload="initJScript()"> <div id="testdiv"> <h1> Überschrift</h1> </div> </body></html>[font=arial,helvetica,sans-serif][\codebox][/font] This is the error message from firebugNS_ERROR_XPC_BAD_CONVERT_JS: Could not convert JavaScript argument arg 0 [nsIDOMHTMLDocument.appendChild][font=arial,helvetica,sans-serif]Thanks a lot for your help[/font]
How to add custom javascript objects to DOM?
in JavaScript
Posted · Edited by helmut
Hi justsomeguy, thanks at first for your answer. This whole DOM extension discussion seems quite complicated to me and ok, I won't extend the DOM.However, I wonder, if your suggestion to create a custom object is an "allowed" extension of the DOM. If I follow your suggestion to cerate a custom element, I would need to combine this new DOM element with the "new" operator. Is there a way to do this?