/    Sign up×
Community /Pin to ProfileBookmark

no histroy(-1) allowed?

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?

to post a comment
JavaScript

15 Comments(s)

Copy linkTweet thisAlerts:
@NevermoreFeb 12.2003 — 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.
Copy linkTweet thisAlerts:
@LAwebTekauthorFeb 12.2003 — That is a good idea and if no one knows of a way to script it I will do it that way.

Thanks!

LAwebTek
Copy linkTweet thisAlerts:
@Sergey_SmirnovFeb 12.2003 — 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>
Copy linkTweet thisAlerts:
@LAwebTekauthorFeb 12.2003 — 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.
Copy linkTweet thisAlerts:
@Sergey_SmirnovFeb 12.2003 — 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.
Copy linkTweet thisAlerts:
@LAwebTekauthorFeb 12.2003 — 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?
Copy linkTweet thisAlerts:
@Sergey_SmirnovFeb 12.2003 — '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.
Copy linkTweet thisAlerts:
@LAwebTekauthorFeb 12.2003 — 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?
Copy linkTweet thisAlerts:
@NevermoreFeb 12.2003 — 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.
Copy linkTweet thisAlerts:
@NevermoreFeb 12.2003 — I didnt put that very well, did I? If you don't understand what i mean, PM me.
Copy linkTweet thisAlerts:
@NevermoreFeb 12.2003 — Create a splash screen which will display instead of your original page, and make it blank. In the head, put

[CODE]
<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>
[/CODE]


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:

[CODE]<a ID="backLink">back</a>[/CODE]
Copy linkTweet thisAlerts:
@NevermoreFeb 12.2003 — oops, got that wrong.

Where it said nodeValue, it should have said innerHTML
Copy linkTweet thisAlerts:
@pyroFeb 12.2003 — cijori, you can edit your posts by hitting edit directly underneath the post. It comes in handy if you see a mistake. ?
Copy linkTweet thisAlerts:
@LAwebTekauthorFeb 12.2003 — 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.

?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)
Copy linkTweet thisAlerts:
@LAwebTekauthorFeb 12.2003 — 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()">
×

Success!

Help @LAwebTek spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...