Click to See Complete Forum and Search --> : no histroy(-1) allowed?
LAwebTek
02-12-2003, 11:19 AM
I'm passing content to my page using a div and the innerHTML attribute, but if a visitor presses the back button they will most likely leave the site (because it's all on one page). How can I keep this from happening? I was thinking something like onUnload=function()
function() {
if(document.location.history(-1)=true){
document.loaction.history(+1)
}
}
I know that doesnt work but is there a similar way?
Nevermore
02-12-2003, 11:27 AM
You could use a splash screen, actually. Make a page as your index that automatically immmediately lodas your other page. Then, if they hit back, that page will send them back.
LAwebTek
02-12-2003, 11:29 AM
That is a good idea and if no one knows of a way to script it I will do it that way.
Thanks!
LAwebTek
Sergey Smirnov
02-12-2003, 11:32 AM
This is a example of some substitute for splash page.
<html>
<head>
<script>
window.history.forward();
function toPage() {
document.location.href="main_page"
}
</script>
</head>
<body onload="toPage()">
</body>
LAwebTek
02-12-2003, 11:33 AM
I know how to redirect from one page to another, I want to be able to make a page redirect to itself if the back button is pressed.
Sergey Smirnov
02-12-2003, 11:38 AM
You can not trap it. If user clicks back button, the next check will be on the previous page in history. If this page is not yours, you are out of business. So, You have to have YOUR previous page. The sample of this page is above.
Read cijori comment. cijori is 100% right.
LAwebTek
02-12-2003, 12:02 PM
what if I call goHome onUnload:
function goHome()
{
var goTo=confirm('You are leaving the LAwebTek site. Continue?');
if (goTo==true)
{
}
else
{
window.open('sendback.html','Sendback','width=0,height=0');
}
}
Then could I script sendback.html to redirect the parent window to my index page and then clode itself?
Sergey Smirnov
02-12-2003, 12:26 PM
'width=0,height=0' does not work in any browser, because of the security reason. So, user will see the window. I guess, you cannot change window.opener.location if previous page in the history is not yours (has difference domain). It is the same reason.
LAwebTek
02-12-2003, 01:33 PM
The problem still exists in this solution that every time the back button is pressed the user will get sent back to my site. I don't want to trap them, which is why I was trying to base it on a confirm result. Is there no way to make the browser think that history(-1) is the same as the current page?
Nevermore
02-12-2003, 01:43 PM
Is the problem that people are leaving accidentally, or are you trying to stop them leaving at all?
If you go with my splash screen idea, you CAN disable the back button, quite simply. If you have the splashscreen open a new window with your page in immediately, then close itself, the back button will be grayed out. This would prevent use of the back button AT ALL, though, so could annoy visitors. Is that the sort of thing you were looking for? If that effect is what you were looking for, it is the only way to do it because of how IE and Netscape work.
You could even provide a back button on your site, and have the splash screen pass along the referrer URL as if it were posting a form, and have it posted as a hidden value. Then you could have a link at the top called something like "leave my site". That would be quite a complex solution, but i think it should work.
Nevermore
02-12-2003, 01:45 PM
I didnt put that very well, did I? If you don't understand what i mean, PM me.
Nevermore
02-12-2003, 02:04 PM
Create a splash screen which will display instead of your original page, and make it blank. In the head, put
<script language="JavaScript">
referrer=document.referrer;
secondWindow=window.open('newpage.htm', 'New Page', '');
secondWindow.getElementById("backLink").firstChild.nodeValue='<a href="' + referrer + '">Leave My Site </a>
</script>
In the document you want to stop people getting out of so easily, put this where you want the leave my site link to be:
<a ID="backLink">back</a>
Nevermore
02-12-2003, 02:10 PM
oops, got that wrong.
Where it said nodeValue, it should have said innerHTML
cijori, you can edit your posts by hitting edit directly underneath the post. It comes in handy if you see a mistake. :D
LAwebTek
02-12-2003, 02:59 PM
ok I think I found a solution from reading your posts. What I can do is create the splash page with a script that does 2 things:
A)count visits to the splash page - on first visit simply redirect to my homepage.
B)on visit > 1 confirm "Do you want to leave?"
yes returns history(-3)?? then deletes the cookie
no returns redirect to homepage.
This way the user is not trapped and does not have to look for a button to let them out.
Thanks to all who posted and helped with this!!
(and thanks to Pyro for pointing out that edit button, never noticed it before, lol)
LAwebTek
02-12-2003, 04:09 PM
Here's the solution that worked for me:
<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 anything but works
if (count==null) {
setCookie('counter',1,expdate)
document.location.href='http://www.lawebtek.com'
}
else {
var goTo=confirm('You are leaving the LAwebTek site. '
+ 'Continue?');
if (goTo==true) {
delCookie('counter')
window.history.go(-1)
}
else
{
document.location.href='http://www.lawebtek.com'
}
}
}
</script>
</head>
<body onLoad="goHome()">