Is it possible to navigate to particular URL on window.unload? - javascript

Is it possible to navigate to particular URL on window.unload?
Something like below.......target browser is chrome and safari.
$(window).on('beforeunload', function () {
window.location.href = "default.aspx";
});

Add this to the <head> tag.
<script>
window.onbeforeunload = function () {
window.location = 'http://www.example.com';
return false;
}
</script>
Or if you have an external js, that'd do too.

Related

prevent redirects if not on same domain (javascript)

Is there a way to prevent redirects if they aren't on the same domain? There's this snippet that works well:
window.onbeforeunload = function() {
return true;
}
But the problem is that it prompts user on every redirect. I would like to truncate prompts only to different domain redirects. I've tried:
window.addEventListener('beforeunload', function(e) {
if (!location.href.match('domain')) {
window.onbeforeunload = function() {
return true;
}
}
and
window.onhashchange = function() {
if(!location.href.match('domain')) {
window.onbeforeunload = function() {
return true;
};
}
}
and plenty of other variations but they don't seem to work. Did anyone have a similar problem?
Try with window.location's property hostname:
window.onbeforeunload = function() {
return window.location.hostname !== 'example.com'; // your domain
};
Long story short: using JS it isn't possible to know what the next adress is so it can't be done due to privacy policy.

Page redirection on page refresh not working

I want to redirect to a page on page refresh. Below is my script.
$(window).unload(function () {
alert("exiting");
window.location = 'http://www.google.com';
return false;
});
Its not working. I don't know what I am missing.
Most of the browser cannot handle anything after unload process is done.
You should use beforeunload event instead.
$(window).bind('beforeunload', function() {
alert("exiting");
window.location = 'http://www.google.com';
return false;
});
try this :)
$(window).bind('beforeunload',function(){
alert("exiting");
window.location = 'http://www.google.com';
return false;
});

How to relocate to another page when the user refreshes the browser?

I'm looking to have the following functionality. When the user refreshes the page I want it to go to a certain page Eg:
$(window).bind('beforeunload',function(){
window.location.href = "#splash";
});
Now the .bind() works because if I say return 'Really?'; it opens up a dialog saying Really? But I would like the page to be changed. How would I do this?
i have found next tip (tested in latest chrome):
var flag = true;
function confirmExit()
{
if (flag)
setTimeout(function(){flag=false;location = '/index.html';});
}
window.onbeforeunload = confirmExit;
Try this one.
$(window).bind('beforeunload',function(e){
e.preventDefault();
window.location.href = "#splash";
});
Use:
function confirmExit()
{
alert("exiting");
window.location.href='index.html';
return true;
}
window.onbeforeunload = confirmExit;

window.onbeforeunload executed on page refresh instead of on page close

I'm using window.onbeforeunload to pop up a confirm dialog when a close event occurs, but the confirm dialog appears on page refresh and doesn't execute on page close.
Here's the JavaScript code:
<script language="JavaScript">
window.onbeforeunload = confirmWinClose();
function confirmWinClose() {
var confirmClose = confirm('Close?');
return confirmClose;
}
</script>
I tried it on Chrome, Firefox and Internet Explorer.
PROBLEM WITH YOUR CODE:
the function will be called when you refresh, because on refresh the page is unloaded and then reloaded.
in your solution, you should also note that you are not assigning a function to window.onbeforeunload but you are assigning the return value of the function when you write
window.onbeforeunload = confirmWinClose();
which might also execute the function (based on where you place it in the javascript) whenever the assignment is done. For e.g.
function confirmWinClose() {
var confirmClose = confirm('Close?');
return confirmClose;
}
window.onbeforeunload = confirmWinClose();
the above code will execute the confirmWinClose function whenever this js is loaded.
(not your case as you have defined the function after call, so won't be executed on load, but you should remember this)
SOLUTION:
the below solution is working for close also
instead of your solution, i tried this
JS:
window.onbeforeunload = function() {
var confirmClose = confirm('Close?');
return confirmClose;
}
or
window.onbeforeunload = confirmWinClose; //note the absence of calling parantheses
function confirmWinClose() {
var confirmClose = confirm('Close?');
return confirmClose;
}
this works as expected.
also note that you should return from the onbeforeunload explicitly.
even if you have to call a function, you should do
<script>
window.onbeforeunload = function(e) {
callSomeFunction();
return null;
};
</script>
No full solution, but you can intercept the F5 key (not intercepted if the user click on the refresh browser button...)
var isRefresh = false;
// with jquery
$(function() {
$(document).on("keydown", function(e) {
if (e.which === 116)
{
isRefresh = true;
}
});
});
window.onbeforeunload = function() {
if (! isRefresh)
{
return confirm ('Close ?');
}
return false;
};
Since you anyway only want to display your text, What just using the onbeforeunload as it is expected just return the string?
<script language="JavaScript">
window.onbeforeunload = confirmWinClose;
function confirmWinClose() {return "close?";}
</script>
Try this it will work. You were assigning the method to onload that need to execute when it event occur so its need to be like object. refer link for better explanation - var functionName = function() {} vs function functionName() {}
window.onbeforeunload = confirmWinClose;
function confirmWinClose () {
var confirmClose = confirm('Close?');
return confirmClose;
};

Problem in my javascript

The page URL has to change when the loadTest() function is called on a ahref click. But the page is getting refreshed only when alert is included in the code. If I remove the alert the same page is getting refreshed and not the new URL which I need.
<script type="text/javascript">
function loadText(prod_id)
{
curUrl = document.location.href.split("?");
document.location = curUrl[0]+'?prodId='+prod_id;
alert("Test");
}
</script>
use window.location instead of document.location
function loadText(prod_id)
{
curUrl = window.location.split("?");
window.location = curUrl[0]+'?prodId='+prod_id;
}
window.location = curUrl[0]+'?prodId='+prod_id;

Categories