Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open window with dynamic content

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!

like image 714
julio Avatar asked Jan 28 '26 16:01

julio


1 Answers

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.

like image 173
Pointy Avatar answered Jan 31 '26 05:01

Pointy