2

Is it possible to open a window from PHP that has predefined content? It's obvious how you can open a window from a javascript link that frames an existing page, or just do a target=_blank from a regular a tag that references an existing page. But I am generating a bit of content, and want that content to be opened in a new link (or streamed to the viewer)--

something like (clearly psuedo code!):

$content = "Hello World. <br />Nice to meet you!";

<a href="#" target="_blank" content=$content>Open up!</a>

Is this possible? Thanks!

1 Answer 1

11

Well, the direct answer to your question is that you can't really do it from PHP directly, because it's the browser that's going to open the window. You can, however, have your page open a window, get the document object, and write to it:

var w = window.open("Surprise", "#");
var d = w.document.open();
d.write("<!DOCTYPE html><html><body>Hello World</body></html>");
d.close();

Instead of the simple string "Hello World" of course your PHP script can put together whatever it wants. Additionally, if desired, the Javascript code itself can dynamically generate the content based on page status, form fields, etc.

Note that you can't guarantee that the new window won't be a new tab, which is no different than what happens with "target" in <a> or <form> tags.

edit — oh also: if you try to use window.open outside of code that's running in response to a "click", browsers will probably think you're trying to show a pop-up ad and will block it.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks pointy! I ended up passing the data as a post and then just parsing the post data, but I'll give your method a shot, it looks much cleaner. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.