Do something on window.onpopstate or window.onload but never twice
I would like to write a javascript code able to do something on either
window.onpopstate or window.onload but never twice (note that chrome fires
a popstate event both onload and when clicking the back navigation button)
I would like it to be compatible for major modern browsers (IE10 firefox
chrome opera safari)
And i would prefer NOT to use any library - not even jquery.
Something like this (but with the right syntax of course):
window.onload || window.onpopstate = function(){
window.alert("hello world");
}
Thanks
EDIT
With ur suggestions i have tried this:
var ranAlready=false;
window.onload = function(){
if (!ranAlready){
window.alert("onload was true");
ranAlready=true;
}
};
window.onpopstate = function(){
if (!ranAlready){
window.alert("popstate was true");
ranAlready=true;
}
};
But now im not sure how to set the variable back to false.. cuz it depends
on the browser and so on..
No comments:
Post a Comment