DeadHeat Posted March 31, 2015 Share Posted March 31, 2015 I need some help I have to create a button on a web page that opens a web page of the html code written in the first web page. I'm really stuck any ideas? Link to comment Share on other sites More sharing options...
Ingolme Posted March 31, 2015 Share Posted March 31, 2015 Do you mean to display the code of the first page as plain text in a new window? Link to comment Share on other sites More sharing options...
DeadHeat Posted March 31, 2015 Author Share Posted March 31, 2015 No I mean create a web page based off of the html code that the user writes in the textarea in the first webpage. So you have one web page with a textarea in it, the user writes html code in the textarea and when he clicks a button the code he wrote is used to create another webpage. Link to comment Share on other sites More sharing options...
Ingolme Posted March 31, 2015 Share Posted March 31, 2015 Yes, it can be done with Javascript. First, you need to access the textarea, using document.getElementById() and get its value, then open a new window and write to its document. The Javascript function would look something like this: function showHTML() { var w = window.open(); w.document.open(); w.document.write(document.getElementById("myTextarea").value); w.document.close();} You could call the function when a button is clicked. Here's a similar example on the W3Schools website, but without getting the HTML from a textarea: http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_doc_open2 Link to comment Share on other sites More sharing options...
DeadHeat Posted March 31, 2015 Author Share Posted March 31, 2015 Thanks I really appreciate this Link to comment Share on other sites More sharing options...
DeadHeat Posted March 31, 2015 Author Share Posted March 31, 2015 function closeHTML() {o.document.close();} I'm trying to use the code above to create a separate button to close the window but so far no luck. Link to comment Share on other sites More sharing options...
Ingolme Posted March 31, 2015 Share Posted March 31, 2015 To close the window you call close() on the window object, not on the document. You'll also need a global reference to the window you created, right now, "w" is local to the function and can't be used outside of it. Link to comment Share on other sites More sharing options...
DeadHeat Posted March 31, 2015 Author Share Posted March 31, 2015 I tried that close() on the object and it didn't work. Link to comment Share on other sites More sharing options...
Ingolme Posted March 31, 2015 Share Posted March 31, 2015 It won't work if the variable that contains the new window is local to the function, it needs to be accessible from other scopes. What code did you try? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now