Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can javascript write an html page and then open it in a broswer?

    I wanted to know if I can make a html page with document.write and then open it in a browers window lets say IE? Anyone know how I can do this?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by cmasterd View Post
    I wanted to know if I can make a html page with document.write and then open it in a browers window lets say IE? Anyone know how I can do this?
    Yes, it is possible. document.write() can be used to write the original content of the page (but not to update it after the page has loaded). But it is strongly contra-indicated, as the Americans say. Obviously the browser must be running and Javascript must be enabled before any document.write() statements can be executed.

    What are you actually trying to do? What exactly is the point? What would that achieve?
    The most peculiar and bizarre requests seem to come from complete newcomers.


    It is your responsibility to die() if necessary….. - PHP Manual
    Last edited by Philip M; 11-21-2012 at 04:33 PM. Reason: Typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,637
    Thanks
    6
    Thanked 1,004 Times in 977 Posts
    Also, JS availability aside, it’s a pretty complicated way to construct a website like that. I’m also curious what the intention is.

  • #4
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Pretty much its for testing, the end game is that I can put in a file path to a html page which is local. Within that html page is a div tag that I want to take and put into the js made html page. Its so that I can take a div that that changes all the time and place it in a test page that never changes. So I know how it will look on a local version of the live site. It would just be a handy tool.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    Using any server side language you can do the same thing in a way that works for everyone and not just those with JavaScript.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    <shrug>You could do this using document.write, but why?</shrug>

    If all you need to do is clone the contents of a <div> into another window, there's no need to use document.write.

    Let's say you have a basically blank HTML page that looks like this:
    Code:
    <html>
    <body>
    <div id="putItHere"></div>
    </body>
    </html>
    Let's name that HTML file "myBlank.html".

    So now, from your main page, you could do something like this:
    Code:
    <html>
    <body>
    <div id="whatever"> 
       Any content at all.
       <div> even an embedded div </div>
    </div>
    
    <input type="button" id="doit" value="Clone!"/>
    
    <script type="text/javascript">
    (
      function( )
      {
          document.getElementBYId("doit").onclick = cloneIt;
    
          function cloneIt( )
          {
              var w = window.open( "myBlank.html", "_blank" );
              w.onload = 
                function( )
                (
                    document.getElementById("putItHere").innerHTML = 
                         window.opener.document.getElementById("whatever").innerHTML;
               }
          }
      }
    )( );
    </script>
    </body>
    </html>
    Not the best possible way, but it should work. And certainly better then document.write().
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I wanted it to work with a c++ program which lets you preview the html page you want to take the div tag out of and then it runs the javascript which place it into a template html page. I thought that if the JS just set up the template page it would be easier rather then taking it out of the first page opening a second page and place it in it.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    Quote Originally Posted by cmasterd View Post
    I wanted it to work with a c++ program which lets you preview the html page you want to take the div tag out of
    So why not have the C++ do the whole thing rather than using JavaScript to do something that is better done on the server.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Good way of learning Javascript?

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Debatable.

    What you are trying to do isn't something one would normally use JS for.

    Even the code I provided, using innerHTML to copy the contents of a <div>, isn't best case JavaScript usage. And document.write() most certainly isn't.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    Quote Originally Posted by cmasterd View Post
    Good way of learning Javascript?
    Worst possible way since the commands you would need to use are basically obsolete in JavaScript having been replaced by server side coding a long time ago.

    As server side processing can handle almost everything prior to the web page finishing loading, modern JavaScript assumes that the HTML has already loaded prior to any of it running and trying to interact with the page.

    You should always use the language and language constructs intended for a particular task. There are enough legitimate uses for JavaScript and the particular commands in any language to not need to "invent" situations to learn them that in the real world ought to be handled either by a different language or a different command.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •