php javascript new window

Posted on 2014-07-15
Last Modified: 2014-07-17
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.

Question by:fpoyavo
    LVL 21

    Expert Comment

    by:Randy Poole
    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?
    LVL 1

    Author Comment


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

    LVL 107

    Expert Comment

    by:Ray Paseur
    ...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.
    LVL 13

    Expert Comment

    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 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" "">
    <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

    LVL 107

    Expert Comment

    by:Ray Paseur
    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.
    LVL 58

    Accepted Solution

    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="//"></script>
    var childwindow 
    	childwindow =$(this).attr("href"), '', 'height=200,width=400');
    	childwindow .onbeforeunload = function () {
    		alert("Child Closed")

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now