[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 470
  • Last Modified:

php javascript new window

Hi Experts,

I have simple php page with just loop in it. What I need to do is to open popup window [not my url .. can be anything meaning I just have url I don't create that page] as a child window from php page then in that loop to wait until this child window is closed and continue my loop.

1 Solution
Randy PooleCommented:
php is executed on the server and the output is pushed to the client so this is not possible. What are you trying to accomplish?
fpoyavoAuthor Commented:

I am trying exactly what I wrote before. The server code and client code can communicate as you know.

Ray PaseurCommented:
...wait until this child window is closed and continue my loop
I don't know of any "window closed" signal that comes from the client to the server, and there is nothing I have ever seen that allows the server to wait for the client, other than the HTTP protocol for client/server communications.  The problem with trying to get this information goes to the essential nature of the HTTP client/server protocol.  Clients make requests, and servers make responses.  The request is atomic, complete and stateless.  The server response is based solely on the client request, is complete and usually instantaneous.  And that's it - nothing more.  There is no "back-and-forth" at all -- just requests and responses.  That said, if you want to show us a web site that does what you're describing, I'll be glad to examine it and tell you how it works.  Or if you want to give us a concrete example with more details and less theory we might be able to suggest a common design pattern.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

You might just need javascript code only as follows for reference only not fully tested.
If you still want to include javascript code in php, you need to
put it by echo , for  exampel,  <?php echo "<script type.....function init()......</script>...."; ?>
Probably, your issue will be solved just by javascript.
When the html page is running or on loading, it will run init() function in whcih
yahoo.com will open in pop window and the timer by window.setInterval will
check the pop window is closed or not for 5 seconds. If closed, it will
display message "Close Pop Window" at div tag with id of "mess"

Hope understand your question completely.If not, please point it out
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Pop window javascript</title>
<script type="text/javascript" >
function init(){

if (win.closed== true) { // !== is required for compatibility with Opera
        document.getElementById("mess").innerHTML="Close Pop Window";
<body onload="javascript:init();">
<div id="mess">Open Pop Window</div>

Open in new window

Ray PaseurCommented:
See if this helps you understand the issues.

jQuery had an unload event handler a long time ago, but it was deprecated at jQuery 1.8, probably because it behaved unpredictably and fired on a number of counter-intuitive events.  For example, it would fire when the client followed a link or gave focus to a different window.  There is a beforeunload event handler, but a careful reading of the literature indicates that has numerous problems, too (quirky cross browser implementation, fails on iPad, etc).

Executive summary: You cannot reliably detect or manipulate (human) client behavior, unless the client makes an HTTP request.  Closing a window does not trigger an HTTP request.  In some browsers, closing a window may trigger an event handler, but this event handler is shared by many other events.  So this design sounds like a recipe for confusion.
If the child window was loading a url that you have control over then you can capture when the child window is closed.
But if the url is a domain you do not control then it is not possible due to cross domain scripting security

Sample for same url (using jquery)

<a href="#">Click to open child window</a>

<script type="text/javascript" src="//code.jquery.com/jquery-latest.min.js"></script>

var childwindow 
	childwindow = window.open($(this).attr("href"), '', 'height=200,width=400');
	childwindow .onbeforeunload = function () {
		alert("Child Closed")

Open in new window


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now