LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2006, 09:31 AM   #1
dlublink
Member
 
Registered: Oct 2004
Location: Canada
Distribution: Ubuntu
Posts: 329

Rep: Reputation: 30
Writing to HTML document after loading completes


Hi,

I have a function that is called to add content anywhere in an HTML document. I used document.write. This works find when the document is loaded, but if I call the function when the document is done loading I lose everything.

I have found a lot of websites warning that this will happen, but I can't seem to find a solution to this issue.

The closest thing I found was innerHTML, but I want to add code anywhere on the page. So I suppose as long as it goes into the body it's fine.

Any thoughts on the best way to do this?

Thanks,

David
 
Old 09-13-2006, 09:38 AM   #2
dlublink
Member
 
Registered: Oct 2004
Location: Canada
Distribution: Ubuntu
Posts: 329

Original Poster
Rep: Reputation: 30
Where the variable html contains the content to be added, I tried the following code:

var div = new HTMLDivElement();
div.innerHTML=html;
document.body.appendChild(div);

But it gives an error:
Error: uncaught exception: [Exception... "Object cannot be created in this context" code: "9" nsresult: "0x80530009 (NS_ERROR_DOM_NOT_SUPPORTED_ERR)" location: "http://mydomain.tld/folder/windows.js Line: 45"]
 
Old 09-13-2006, 09:40 AM   #3
dlublink
Member
 
Registered: Oct 2004
Location: Canada
Distribution: Ubuntu
Posts: 329

Original Poster
Rep: Reputation: 30
Well, I found a way to do it. I am sure there is a better way, but here is what I did:

I replaced:
document.write(html);

with:

document.body.innerHTML=document.body.innerHTML + html;

It works during page loading and after the page is loaded!

As I said, I am sure there is a better way to do it, any suggestions?

David
 
Old 09-13-2006, 09:46 AM   #4
dlublink
Member
 
Registered: Oct 2004
Location: Canada
Distribution: Ubuntu
Posts: 329

Original Poster
Rep: Reputation: 30
Alright, I managed to get the HTML inserted, but the above call causes other scripts on the page to malfunction for some reason.

I think it is causing the entire body to be reloaded which crashes my scripts...

Any other suggestions on how to do this?

I don't have access to the original HTML document, I only have access to the javascript...

David
 
Old 09-13-2006, 09:53 AM   #5
dlublink
Member
 
Registered: Oct 2004
Location: Canada
Distribution: Ubuntu
Posts: 329

Original Poster
Rep: Reputation: 30
Alright, I think I found the proper way to do it:


var html;
// assign something to html
var div=document.createElement('div');
div.innerHTML=html;
document.body.appendChild(div);

If I do simple text or something without positions it doesn't appear, but if I added positions in my style attribute I see the html.

For my purposes this works.

David
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
writing mathematical expression in a document mohtasham1983 Linux - Software 1 05-02-2006 11:56 AM
How to get a cohesive document from a series of HTML pages rickh Linux - Software 4 03-04-2006 08:22 PM
changeing default document from index.html in apache thebover Linux - Software 3 03-30-2005 10:26 PM
simple q: can you use php inside an html document? BrianK Programming 2 12-10-2004 10:31 PM
Fooling a HTML document? eantoranz Programming 11 11-11-2004 08:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration