Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-17-2012, 06:52 AM   PM User | #1
djh101
Regular Coder

 
djh101's Avatar
 
Join Date: May 2009
Location: Santa Clarita
Posts: 607
Thanks: 48
Thanked 63 Times in 63 Posts
djh101 is an unknown quantity at this point
document.body.innerHTML and Javascript

I'm currently working on an HTML executor and would like users to be able to put javascript in their code if they wish to do so. Currently I have their input sent to an iframe via document.body.innerHTML. The problem, though, is that user submitted javascript doesn't work (with the exception of anything placed in the onload attribute of an image). Any suggestions?
__________________
"Yeah science!"
Online Science Tools

Last edited by djh101; 01-17-2012 at 08:29 AM..
djh101 is offline   Reply With Quote
Old 01-17-2012, 07:04 AM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
text nodes?
xelawho is offline   Reply With Quote
Old 01-17-2012, 07:32 AM   PM User | #3
djh101
Regular Coder

 
djh101's Avatar
 
Join Date: May 2009
Location: Santa Clarita
Posts: 607
Thanks: 48
Thanked 63 Times in 63 Posts
djh101 is an unknown quantity at this point
What about text nodes?
__________________
"Yeah science!"
Online Science Tools
djh101 is offline   Reply With Quote
Old 01-17-2012, 07:35 AM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
weren't you looking for suggestions? I thought I read that somewhere.
xelawho is offline   Reply With Quote
Old 01-17-2012, 07:48 AM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,532
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
You can't add JavaScript using innerHTML - you have to use createElement('script') to add a script into a web page that will then be actually run.

innerHTML basically inserts text into the web page - that some of it is HTML tags means that the browser will apply the CSS applicable to those tags to their content however the entire content is really only text as far as JavaScript is concerned - to be able to interact with it from JavaScript you have to build the DOM properly.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 01-17-2012, 08:28 AM   PM User | #6
djh101
Regular Coder

 
djh101's Avatar
 
Join Date: May 2009
Location: Santa Clarita
Posts: 607
Thanks: 48
Thanked 63 Times in 63 Posts
djh101 is an unknown quantity at this point
document.write seems to work just fine. Isn't it lovely when you solve your own problems? 8)
__________________
"Yeah science!"
Online Science Tools
djh101 is offline   Reply With Quote
Old 01-17-2012, 08:56 AM   PM User | #7
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,532
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by djh101 View Post
document.write seems to work just fine. Isn't it lovely when you solve your own problems? 8)
But in solving that one problem that way you now have a hundred more just waiting to happen the next time you touch the code in the page. There are lots of reasons why you should never use document.write in a script running against a web page and not knowing how to write JavaScript properly is the only reason now for doing so.

Unless you still run Netscape 4 you should consider document.write to be dead and gone as that is the most recent browser that requires that you use such messy obtrusive statements as document.write - that call is now only needed in userscripts that are generating new web pages from someone else's web page that you have loaded into your browser (such as a dynamic view source that shows the way the source looks after the JavaScript has updated it).

If the different ways JavaScript can update the web page were dates then the DOM would be 2012, innerHTML would be 2010 and document.write would be 12 billion BC.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Last edited by felgall; 01-17-2012 at 09:05 AM..
felgall is offline   Reply With Quote
Old 01-17-2012, 09:16 AM   PM User | #8
djh101
Regular Coder

 
djh101's Avatar
 
Join Date: May 2009
Location: Santa Clarita
Posts: 607
Thanks: 48
Thanked 63 Times in 63 Posts
djh101 is an unknown quantity at this point
I suppose you're right. I was hoping for a nice one line solution, but those don't always exist.
__________________
"Yeah science!"
Online Science Tools
djh101 is offline   Reply With Quote
Old 01-17-2012, 09:24 AM   PM User | #9
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,532
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by djh101 View Post
I was hoping for a nice one line solution, but those don't always exist.
Any hugely complex piece of JavaScript written by someone else and contained within a function effectively is a nice one line solution at least as far as the code you need to write is concerned. So all you need to do is to find code that someone else has made available for others to use that does what you want and call that.

The same applies to any function you have already written yourself the second time you need to use it.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 01-18-2012, 07:52 AM   PM User | #10
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,532
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Take a look at http://ejohn.org/blog/pure-javascript-html-parser/ - The JavaScript there written by John Resig (who also wrote jQuery) provides you with a one line solution that just calls his code

for example instead of

document.getElementById('test').innerHTML = '<script ...';

which doesn't work you would substitute

HTMLtoDOM('<script ...', document.getElementById("test"));

which would parse the HTML and add it to the web page properly using the DOM therefore allowing script calls within the code to run and any subsequently run scripts to interact with the content properly.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
djh101 (01-18-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:13 AM.


Advertisement
Log in to turn off these ads.