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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Open new browser window using Javascript

    Hi

    I am a new bie to Javascript programming. We have a scenario to launch a new browser window for a given HTML. I have the following approaches :

    Approach 1:
    my_window = window.open("", "report");
    my_window.document.open();
    my_window.document.write("<HTML> <BODY> HI </BODY></HTML>");
    my_window.document.close();

    Approach 2:
    var newWindow = window.open("", "report");
    newWindow.document.body.innerHTML = "<HTML> <BODY> HI </BODY></HTML>";

    Could anyone sugges Which is the correct approach ? Our requirement is to have this code to work in any browser?

    Thanks

  • #2
    Regular Coder
    Join Date
    Jan 2006
    Posts
    568
    Thanks
    6
    Thanked 84 Times in 84 Posts
    I'm not sure this will work without an existing document to open. Could you use a blank html page with the html and body tags already present?

    The second example will be incorrect because there shouldn't be a <body> for non-existent pages, and if there was you would be putting an html element inside it.

    What I would do:

    Code:
    var newWindow = window.open("empty.html", "report");
    newWindow.document.body.innerHTML = "HI";

  • #3
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi

    Yes I am launching this code from an existing window.

    The first approach is working fine in IE, Firefox and Chrome. but the second approach is not workin in Firefox and working fine in the rest.

    Hence i would like to know which is the best approach and corresponding pros and cons?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    I think Gus gave you the best approach.

    Use a *real* HTML page and launch it and then use DOM methods to change its content.

    But if you aren't willing to do that, stick with the document.write approach.
    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.

  • #5
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    If the second page's content is static, it would easier to just create a second (child) html page with the content you want and then use window.open() in the parent window to open the child page.

    Code:
    window.open('childPage.htm');

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    Sure, but even if it's not static, Gus's approach is better.

    Example, say "childPage.htm" looks like this:
    Code:
    <html>
    <body>
    Thanks for answering our survey, <span id="username"></span>.
    <br/><br/>
    We will get back to you by <span id="when"></span>.
    </body>
    </html>
    And then, in the main page, maybe do
    Code:
    var pop = window.open("childPage.html", "Thanks");
    var pw = pop.document;
    pw.getElementById("username").innerHTML = respondentName;
    pw.getElementById("when").innerHTML = oneWeekFromToday;
    Less to document.write, and you can use CSS to nicely style the page at the same time.

    Or you could put a function in childPage.html that receives, say, a couple of arguments and puts them in place on the page where needed.
    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
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Old Pedant View Post
    Sure, but even if it's not static,
    If it's not static then you can't do what I suggested.

    That's why I said

    If the second page's content is static.....
    But in the op's code there is nothing there that couldn't be directly coded into the child html file and you don't need to pass any content code from the parent to the child window. So what are you on about?
    Last edited by webdev1958; 08-28-2011 at 08:09 AM.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    We seem to be using the same word ("static") to mean two different things. My code in post #6 showed what *I* considered to be a *NON*-static web page. Granted, 90% of it is static, but the content varies at least in part, so to me the effect is dynamic. You seem to be calling that a static page. I don't think we can agree on terminology, and in any case we don't know exactly what the original poster was *actually* displaying, so let's just leave it at this. He now has three answers. Let him pick the one that works for him.
    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.


  •  

    Posting Permissions

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