In addition to history.length in JavaScript you can read/write the window's name.
Thus if you check if it has a name onload... it should be blank on the very first load... if you then set it to "foo"... on each subsequent load in that window... the window.name property will return "foo"... unless you open a link in a new tab/window... that new window should have no name set.
(unless of course you open a popup via window.open(url, name, features); which allows you to pre-set the name)
<script>
if(window.name == ''){
//first load (or Nth load in a new Tab/Window)
if(!SOME_VALUE_SET_FOR_2ND_TO_NTH_LOADS){
//set name so we can catch new Tab/Window
window.name = 'myWinName';
} else {
//we have a new Tab/Window (or something funky)
alert('What?! One window not cool enough for ya?\n' +
'Calling the InterWeb Police!');
}
} else if(window.name == 'myWinName'){
//2nd-Nth load
document.title = 'All is well... we think';
}
</script>
Caveats:
- If your page is initially loaded in a window/frame that already had a name... things will get quirky
- If your page has (named) iframes and you have any links targeted into those iframes, there is a bug in IE7/8 whereby when the user opens those links in a new tab/window, the new tab/window will "inherit" the name of the iframe that was originally targeted (very ODD bug with no fix ever expected)