Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to code a exit popup plugin with jquery . It should be similar as this one http://kbeezie.com/view/cross-browser-exit-pop/ the demonstration is here http://kbeezie.com/exitpop.html Can anyone please help me how to code this so it display on all pages and posts. Any type of help/tip will be appreciated. THanks

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Before I give you any code, I want to be very clear that exit popups are frowned upon in pretty much every development community. They're annoying to users, create a bad customer experience, and will tick off just about any developer who ever looks at your site.

I can't say this any more emphatically, but please, please, please do not use exit popups on your site.

OK, now that you've ignored that recommendation ... here's how you'd do it anyway.

Your plugin will consist of two files: plugin.php and plugin.js in this example (please use unique names).

In addition to the standard plugin headers, your plugin.php file must contain code to enqueue your script file:

add_action( 'wp_enqueue_scripts', 'wpa_54303_enqueue_scripts' );
function wpa_54303_enqueue_scripts() {
    wp_enqueue_script( 
        'wpa_54303_script',                   // Unique script name
        plugins_url( '/plugin.js', __FILE__ ) // Location of your script file
    );
}

This code will load your script on every post and page in the header of your site. Now, just place the relevant code from that tutorial you linked to in your plugin.js file:

var popit = true;
window.onbeforeunload = function() { 
    if(popit == true) {
        popit = false;
        return "Are you sure you want to leave?"; 
    }
}

Let me say one more time that I absolutely do not encourage you use this kind of feature in any site. It's a quick way to get your name on the top of many developers' "I don't like this guy" lists.

share|improve this answer
    
Hey! thanks a lot for the answer. Yea i know its annoying but its for someone else. so i had to. Can you please tell if i want to display a div box c if the user wants to stay on the page ? –  Jasminder Pal Singh Jun 5 '12 at 21:54
    
That would be a different question all together, and not really WP-specific. I suggest asking that one on Stack Overflow. –  EAMann Jun 5 '12 at 21:58
    
Done with it :_) –  Jasminder Pal Singh Jun 8 '12 at 8:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.