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

Announcement

Collapse
No announcement yet.

Open new browser window using Javascript

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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";
    Chess

    Comment


    • #3
      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?

      Comment


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

        Comment


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

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

          Comment


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

            Comment


            • #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, 03:09 AM.

              Comment


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

                Comment

                Working...
                X