I was asking the same question a few days ago. Depends on what you are using it for as to the best solution. In my case, My home page is only one html page with a few links. Instead of loading new pages, the links write the new content into a div. The problem is that out of habit people hit the back button which would cause them to accidently leave my site. To solve this I created index.html with the foolowing code which simply redirects to my homepage, index2.html on the first visit and then asks whether or not you really want to leave if you hit back. "OK" lets the visitor out and "Cancel" sends them back to index2.html:
<script language="JavaScript" type="text/javascript">
function setCookie (name, value, expires) {
if (!expires) expires = new Date();
document.cookie = name + "=" + escape (value) +
"; expires=" + expires.toGMTString() + "; path=/";
}
var expdate = new Date();
expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000 * 365));
function getCookie (name) {
var dcookie = document.cookie;
var cname = name + "=";
var clen = dcookie.length;
var cbegin = 0;
while (cbegin < clen) {
var vbegin = cbegin + cname.length;
if (dcookie.substring(cbegin, vbegin) == cname) {
var vend = dcookie.indexOf (";", vbegin);
if (vend == -1) vend = clen;
return unescape(dcookie.substring(vbegin, vend));
}
cbegin = dcookie.indexOf(" ", cbegin) + 1;
if (cbegin == 0) break;
}
return null;
}
function delCookie(name) {
document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/";
}
function goHome() {
var count=getCookie('counter')
/*doesnt really count - the only values are null or 1*/
if (count==null) {
setCookie('counter',1,expdate)
document.location.href='index2.html'
}
else {
var goTo=confirm('You are leaving the LAwebTek site. Continue?');
if (goTo==true) {
delCookie('counter')
window.history.go(-1)
}
else
{
document.location.href='index2.html'
}
}
}
</script>
</head>
<body onLoad="goHome()">