View Full Version : Open new browser window using Javascript

08-27-2011, 03:24 PM

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?


08-27-2011, 04:08 PM
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";

08-27-2011, 04:11 PM

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?

Old Pedant
08-28-2011, 06:35 AM
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.

08-28-2011, 06:51 AM
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.


Old Pedant
08-28-2011, 07:43 AM
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.

08-28-2011, 07:53 AM
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?

Old Pedant
08-28-2011, 08:07 PM
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.