views:

214

answers:

2

I am using jQuery to have a promotional window opening up -say- 5 seconds after a page is loaded. But the effect is lost for people who open the page in a new window or a new tab. When they get to my tab the window will already be open.

Is there a way to get this to fire when people actually start viewing my site?

I was thinking about catching a scroll or something, but people don't get started scrolling immediately and most won't scroll at all. Other than that I am out of ideas.

I am not sure if jQuery offers a solution here... javascript?

Thanks.

+1  A: 

Would putting a div around everything work with an onmouseover event listener? I've never tried it, so I'm not sure whether that would fire or not, but it might be worth a shot.

Chris Thompson
I think that might work. I am going to try this but I am concerned that not everyone would move the mouse over the page once they click the new tab, perhaps even less with the new window. They may even scroll without mousing over the wrapping div and it won't trigger the fuction (just guessing). If I don't find any other way though, this seems like a good option.
Necati
+5  A: 

the following should do the trick .. (jQuery)

<script type="text/javascript">
function initiatePopup(){
    $(window).unbind('blur');
    $(window).unbind('focus');
    // do the popup
};

$(document).ready(
             function(){
                        $(window).focus( initiatePopup ).blur( initiatePopup );
                        // your other functions should go from here on

                       }
                 );
</script>

[EDIT] on OP request..

code edited to make the example all inclusive

[EDIT 2]

The code above has been edited again because we need to handle the blur event as well.. so we take the code for the popup somewhere else in order to not duplicate it inside both events..

[EDIT 3]

if you want to pass parameters to the popup if they are created later on, then change the event binding line to

$(window).focus( function() { initiatePopup(params); } ).blur( function() { initiatePopup(params); );

and of course change the initiatePopup to accept parameters ..

Gaby
I tested this and it works on its own. (I tried a test alert message). I need the whole jQuery stuff (this function and others) starting with document(ready) though. I couldn't get this to work just pasting this in document(ready). I guess this is another jQuery question but I'd think that is easy for you, and could give me a tip while you are at it. Thanks!
Necati
added some more info in original post ... although i just added what you describe ... maybe it was a typo..
Gaby
made some more changes to handle the blur event as it is the one fired when going from page to page ...
Gaby
This works with the alert message as well. However, because my pop-up uses variables that are set after document.(ready) it still doesn't work for my situation. It has to be a window.(focus) provided document.(ready) I guess, right?It would be big help if I had worded the question better (with the specifications involved) but I didn't know about the potential troubles ahead; I am sorry to get you through hoops :(So, what could fix the problem(s) bringing the separate function into the document.(ready) produce? I guess this could the answer would be the last stab at this monster (I hope).
Necati
can you pass the parameters to the popup function ?if so see edit 3 for changes ..
Gaby