0

I want to simulate a click,

$("#genStats").trigger("click");

every time I refresh the page

window.location.reload()

but not on the initial page load.

What's the easiest way of doing this?

John
  • 802
  • 2
  • 9
  • 19
  • Why are you simulating this click? – Alex W Jul 11 '12 at 21:42
  • In theory, if you refreshed, wouldn't it be an initial page load each time..? – ayyp Jul 11 '12 at 21:43
  • 1
    is it acceptable to add a hash to your page? For example, `http://myawesomesite.com#refreshed` (or something more discrete like just `#r` if prefered). – lbstr Jul 11 '12 at 21:45
  • What I'm trying to do is turn on the visibility of a jquery accordion after refreshing the page. it is hidden upon initializing. $("#genStats").trigger("click"); will show the accordion. – John Jul 11 '12 at 21:46
  • yes Ibstr that would be acceptable – John Jul 11 '12 at 21:58
  • @lbstr has a better version of my answer i.e. using cookies. I deleted mine. – Nathan Jul 11 '12 at 22:55

2 Answers2

2

Check out this other SO post: Check if page gets reloaded or refreshed in Javascript

The catch here is that this detects if it is the first time the user has landed on the page. So if they land there and refresh, you will get the desired effect. If they leave and come back, however, it will treat it the same as a refresh. If this is unacceptable, you will need to have a script on all of your other pages that clears this cookie.

Here's what you would do in your case:

function isFirstVisit() {
    if (document.cookie.indexOf('beenhere') === -1) {
        // cookie doesn't exist, create it now
        document.cookie = 'beenhere=1';
        return true;
    }
    return false;
}

$(document).ready(function(){
    if (!isFirstVisit()) {
        $("#genStats").trigger("click");
    }
});

And to remove the cookie on your other pages:

function removeCookie(sKey) {  
    if (!sKey || !this.hasItem(sKey)) { return; }  
    var oExpDate = new Date();  
    oExpDate.setDate(oExpDate.getDate() - 1);  
    document.cookie = escape(sKey) + "=; expires=" + oExpDate.toGMTString() + "; path=/";  
}
removeCookie("beenhere");

Here's a good resource on document.cookie

Community
  • 1
  • 1
lbstr
  • 2,822
  • 18
  • 29
0

Within your javascript code place $(document).ready() function that once the page completely load, it will trigger your click.

domoindal
  • 1,513
  • 2
  • 17
  • 33
  • ya, but he doesn't want to do it on the initial page load. He wants some way of distinguishing between initial page load and refreshes. – lbstr Jul 11 '12 at 21:47
  • ok, reload with a GET variable. www.yourdomain.com/?refreshed=true – domoindal Jul 11 '12 at 21:48