Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to...

Status
Not open for further replies.

andycheese

Programmer
Jun 10, 2002
38
0
0
GB
.. catch the browser going back? I was trying code along the lines of :

if history.back(1)
{
do whatever;
}


but it doesn't work. Is there any way of catching someone clicking the back button in IE?
 
not really - there are several half-solutions but not one foolproof cross-browser method.

in IE, you can use the &quot;onbeforeunload&quot; event handler of the <body> tag or document object.

in all browsers, you can load the site using window.open() so that you can turn off the toolbars. then use script to disable certain keystroke combinations that mimick the back button (though i'm not positive that you can disable things like alt+leftArrow in all browsers)

=========================================================
if (!succeed) try();
-jeff
 
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. &quot;OK&quot; lets the visitor out and &quot;Cancel&quot; sends them back to index2.html:

<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
function setCookie (name, value, expires) {
if (!expires) expires = new Date();
document.cookie = name + &quot;=&quot; + escape (value) +
&quot;; expires=&quot; + expires.toGMTString() + &quot;; path=/&quot;;
}

var expdate = new Date();
expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000 * 365));

function getCookie (name) {
var dcookie = document.cookie;
var cname = name + &quot;=&quot;;
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 (&quot;;&quot;, vbegin);
if (vend == -1) vend = clen;
return unescape(dcookie.substring(vbegin, vend));
}
cbegin = dcookie.indexOf(&quot; &quot;, cbegin) + 1;
if (cbegin == 0) break;
}
return null;
}

function delCookie(name) {
document.cookie = name + &quot;=; expires=Thu, 01-Jan-70 00:00:01 GMT&quot; + &quot;; path=/&quot;;
}

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=&quot;goHome()&quot;>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top