Solved

javascript redirect when onunload

Posted on 2010-08-19
3
941 Views
Last Modified: 2013-11-19
I'd like my page to redirect a user to a specific page onunload. Like this:

<body onUnload="window.location = 'http://www.google.com/'">

but this doesn't really seem to be working. Any ideas?

Thanks
0
Comment
Question by:elliottbenzle
3 Comments
 
LVL 22

Accepted Solution

by:
Mrunal earned 500 total points
ID: 33475897
first try this:

<body onunload="alert('sad')">

if got alert then it should work.

<script>
function Redirect() {
var where=URL TO BE REDIRECTED TO;
window.location=where;
}
</script><body onUnLoad="Redirect()">
0
 
LVL 4

Author Closing Comment

by:elliottbenzle
ID: 33476098
Works great. Thank you.
0
 

Expert Comment

by:audibuild
ID: 33476510
You could always try something like this:

<script>
function closeIt()
{
  window.location='http://google.com';
  return true;
}
window.onbeforeunload = closeIt;
</script>

A potential drawback is that you are prompted with a "Are you sure you want to navigate away from this page?" dialog box.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Angular - data is there but why isn't search working? 51 56
Facebook React and SyntheticEvents 6 46
JS Plugin 4 29
Angular - disable input field on page load 4 10
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now