Web Analytics Made Easy -
StatCounter Open new browser window using Javascript - CodingForum


No announcement yet.

Open new browser window using Javascript

  • Filter
  • Time
  • Show
Clear All
new posts

  • Open new browser window using Javascript


    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.write("<HTML> <BODY> HI </BODY></HTML>");

    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?


  • #2
    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:

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


    • #3

      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
        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.
        Be yourself. No one else is as qualified.


        • #5
          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.



          • #6
            Sure, but even if it's not static, Gus's approach is better.

            Example, say "childPage.htm" looks like this:
            Thanks for answering our survey, <span id="username"></span>.
            We will get back to you by <span id="when"></span>.
            And then, in the main page, maybe do
            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.
            Be yourself. No one else is as qualified.


            • #7
              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; Aug 28, 2011, 02:09 AM.


              • #8
                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.
                Be yourself. No one else is as qualified.