简体   繁体   中英

how to open pop up form on close browser event

I have a form I like to open in pop up windows when user close my website or browser .. I want like to take feedback from users by pop up form like "give your comments how we can improve our website" and then with php I will submit the form and admin of website will get users feedback.

I am stuck here and fail to find JS code that open popup form on close browser event.

can any one help me out.

What have you tried so far?

Here is one approach, but user may be able to disable it, and may be blocked by popup blockers (not to mention a whole ethics debate, but hey... you asked):

From main page:

<script>
  window.onbeforeunload = function()
  {
    var url = "feedback_form.html";
    window.open(url, "_blank", "toolbar=yes, scrollbars=yes, resizable=yes, top=500, left=500, width=400, height=400");
  };
</script>

Confirmed that this code works in IE9, Chrome, and Firefox with the popup blocker disabled.

Usability concerns and other thoughts:

  • This will usually annoy people and is considered evil.
  • This approach relies on popup blocker being disabled (if using a browser that supports it)
  • Feedback is voluntary when sourcing from a crowd (your users) like this. Therefore you should make it more of an option. Many sites do a side-floater or hyperlink for "Add your Feedback" or whatever. I would recommend going this route instead of relying on closing the browser.
  • This code will fire on closing the browser, but will also fire whenever user leaves the page, including POSTing form data and navigation to other pages on your site.
  • One interesting application for this could be to send some metrics to your server via an AJAX call on window close instead (like recording how long user visited page, what actions they performed, etc.) that could be used in addition to the voluntary feedback form to learn more about what your users are doing on the site.
  • It is not uncommon for enterprise intranet web applications to do stuff like this (manipulate browser windows), and often those sites / domains will be added to the trusted sites for the client computer, which may include disabling the popup blocker.

Stop wanting to do that. Imagine how horrible the user experience would be for the whole web if it were possible to do that. If it was already possible, a rampaging mob with flaming torches and pitchforks would demand it be fixed immediately. Be thankful that there is no satisfactory answer to your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM