eaweb
asked on
javascript and browser refresh button
hi
i have the following code in my body
onload="flag=true" onunload="if (flag) window.location='naverror. atm' "
when click the back or forward button it works fine, but when clicking the browser refresh button it doesn't work. if i put an alert to debug code like
--onload="flag=true" onunload="alert(flag); if (flag) window.location='naverror. atm' "--
i get true for onunload but it doesn't execute the window.location='naverror. atm' part
how can i solve this?
i have the following code in my body
onload="flag=true" onunload="if (flag) window.location='naverror.
when click the back or forward button it works fine, but when clicking the browser refresh button it doesn't work. if i put an alert to debug code like
--onload="flag=true" onunload="alert(flag); if (flag) window.location='naverror.
i get true for onunload but it doesn't execute the window.location='naverror.
how can i solve this?
ASKER
yes understood ,but why does for example
onload="flag=true" onunload="alert(flag)" work and
onunload="if (flag) window.location='naverror. atm' " not
when refreshing the browser via the refresh button?
what is the problem with window.location='naverror. atm'?
onload="flag=true" onunload="alert(flag)" work and
onunload="if (flag) window.location='naverror.
when refreshing the browser via the refresh button?
what is the problem with window.location='naverror.
how should it have to work ?? I tell you:
You hit refresh:
site is unloading: javascript tells u to go to page naverror,
site is refreshed for old site so it sets to old one.
If you want to prevent refresh, you will need to use other techniques :(
You hit refresh:
site is unloading: javascript tells u to go to page naverror,
site is refreshed for old site so it sets to old one.
If you want to prevent refresh, you will need to use other techniques :(
ASKER
wilq32,
i have the same onunload code in a xsl and when clicking the refresh browser button it work fine (redirection to the naverror page), but when using it in a html the onunload works but it doesn't execute the * window.location='naverror. atm'* part. everything else like an alert works.
i have the same onunload code in a xsl and when clicking the refresh browser button it work fine (redirection to the naverror page), but when using it in a html the onunload works but it doesn't execute the * window.location='naverror.
Alert will work because it will blok running everything until you hit OK. Changing page wont have an effect because, the next process to do after changing page to new one (via window.location) is to change to old one (by browser, not by javascript - this behaviour is coded in browser). So what I can propose to you is hmm.. Refresh is kind of difficult thing to handle. You can for example use cookies - set them at first enter, and if the cookie is set when refreshing then do window.location on reload page... Hmm hope that helps anyhow :/
ASKER
do you have a code flow example you can help me with in doing
****
set them at first enter, and if the cookie is set when refreshing then do window.location on reload page...
****
****
set them at first enter, and if the cookie is set when refreshing then do window.location on reload page...
****
Hope it helps
<script type="text/javascript">
function checkRefresh()
{
if (document.cookie)
{
document.cookie = "refresher=1;expires=Thu, 01-Jan-1970 00:00:01 GMT";
window.location = 'http://other';
}
else document.cookie = "refresher=1";
}
</script>
<body onload="checkRefresh()">
This is test page - do refresh here!
</body>
ASKER
it seems that onload it goes directly to the naverror page.
and the other issue i get is that form the page with onunload, if i am going to for example google it goes to the naverror page.
isn't there a way to check the browser url befoere unload if it is a url of the applaction you are trying to get with back button or refresh let it go to the naverror page and if the usr is trying to get to a url outside the application to let it proceed?
and the other issue i get is that form the page with onunload, if i am going to for example google it goes to the naverror page.
isn't there a way to check the browser url befoere unload if it is a url of the applaction you are trying to get with back button or refresh let it go to the naverror page and if the usr is trying to get to a url outside the application to let it proceed?
> it seems that onload it goes directly to the naverror page.
hmm naverror because it set window.location = 'http://other' -> you will have to change this parameter to what you need...
> isn't there a way to check the browser url befoere unload if it is a url of the applaction you are trying to get with back button or refresh let it go to the naverror page and if the usr is trying to get to a url outside the application to let it proceed?
I dont understand exacly what you need but:
document.location
holds information about actual url - use it somewhere in code to get behaviour that you need
hmm naverror because it set window.location = 'http://other' -> you will have to change this parameter to what you need...
> isn't there a way to check the browser url befoere unload if it is a url of the applaction you are trying to get with back button or refresh let it go to the naverror page and if the usr is trying to get to a url outside the application to let it proceed?
I dont understand exacly what you need but:
document.location
holds information about actual url - use it somewhere in code to get behaviour that you need
ASKER
wilq32,
when i am on the page that uses the onload="flag=true" onunload="if (flag) window.location='naverror. atm' "
for example the url:
https://www.experts-exchange.com/questions/23278267/javascript-and-browser-refresh-button.html
and from that page i want to go to google
for example the url:
http://www.google.com/
i need to check if the actual browser url contains for example
"experts-exchange.com". if yes go to naverror onunload and if not go to google
document.location give me https://www.experts-exchange.com/questions/23278267/javascript-and-browser-refresh-button.html and not the http://www.google.com/ url i just typed in
when i am on the page that uses the onload="flag=true" onunload="if (flag) window.location='naverror.
for example the url:
https://www.experts-exchange.com/questions/23278267/javascript-and-browser-refresh-button.html
and from that page i want to go to google
for example the url:
http://www.google.com/
i need to check if the actual browser url contains for example
"experts-exchange.com". if yes go to naverror onunload and if not go to google
document.location give me https://www.experts-exchange.com/questions/23278267/javascript-and-browser-refresh-button.html and not the http://www.google.com/ url i just typed in
<script type="text/javascript">
function checkRefresh()
{
if (document.cookie)
{
document.cookie = "refresher=1;expires=Thu, 01-Jan-1970 00:00:01 GMT";
window.location = 'naverror.atm';
}
else document.cookie = "refresher=1";
}
</script>
<body onload="checkRefresh()">
This is test page - do refresh here!
</body>
function checkRefresh()
{
if (document.cookie)
{
document.cookie = "refresher=1;expires=Thu, 01-Jan-1970 00:00:01 GMT";
window.location = 'naverror.atm';
}
else document.cookie = "refresher=1";
}
</script>
<body onload="checkRefresh()">
This is test page - do refresh here!
</body>
Actually if you refresh mine example or hit back to this page it will takes you to naverror.atm. In cases of typing manually google.com from this page nothing happens and it will change page normally.
ASKER
ok,
but is there a javascript to check what have been typed in the browser url address when unloading or before unloading a page.
document.location get the url of the current page not what have been typed in the browser url address bar.
this is what i also need to do.
but is there a javascript to check what have been typed in the browser url address when unloading or before unloading a page.
document.location get the url of the current page not what have been typed in the browser url address bar.
this is what i also need to do.
ASKER
and what if after setting the cookie pageA i go to pageB and do the same checkRefresh() onload . isn't the cookie of pageA still available for pageB, so i will be nevertheless redirected to naverror?
From what I know this is not possible to detect what user typed in URL. There are techniques to retrieve typed URL when entering page, but I assume that is not what you want :)
ASKER
no but if we can work out your above script based on my question in "21300539":
and what if after setting the cookie pageA i go to pageB and do the same checkRefresh() onload . isn't the cookie of pageA still available for pageB, so i will be nevertheless redirected to naverror?
and what if after setting the cookie pageA i go to pageB and do the same checkRefresh() onload . isn't the cookie of pageA still available for pageB, so i will be nevertheless redirected to naverror?
If you set cookie on pageA it will be set as long as you will close browser. If on pageB you check for that cookie you will be automaticly redirected. If you want to have unique visits on all pages and do not have problem with redirecting that might help:
<script type="text/javascript">
function checkRefresh()
{
if (document.cookie == document.location)
{
document.cookie = "refresher=1;expires=Thu, 01-Jan-1970 00:00:01 GMT";
window.location = 'naverror.atm';
}
else document.cookie = document.location;
}
</script>
<body onload="checkRefresh()">
This is test page - do refresh here!
</body>
<script type="text/javascript">
function checkRefresh()
{
if (document.cookie == document.location)
{
document.cookie = "refresher=1;expires=Thu, 01-Jan-1970 00:00:01 GMT";
window.location = 'naverror.atm';
}
else document.cookie = document.location;
}
</script>
<body onload="checkRefresh()">
This is test page - do refresh here!
</body>
ASKER
wilq32,
if i use above code then the back and forward button will not redirect me to naverror page because onload on for example pageA the document.location will not be the same any more in order to redirect me to naverror
if i use above code then the back and forward button will not redirect me to naverror page because onload on for example pageA the document.location will not be the same any more in order to redirect me to naverror
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
wilq32,
thanks for your last comment. it really helped. i use a asp session variable to set it to false and when the page load i set it back to true. so when clicking on a link i call a javascript function that uses a http background page call to a asp page that set the variable to false in order for the next page to load. if the session variable is true i redirect the user to the naverror page
thanks for your last comment. it really helped. i use a asp session variable to set it to false and when the page load i set it back to true. so when clicking on a link i call a javascript function that uses a http background page call to a asp page that set the variable to false in order for the next page to load. if the session variable is true i redirect the user to the naverror page
Microsoft's documentation explictly says this is what will happen.
The onunload() method was originally conceived as a way to run a short clean-up script that would undo any lingering effects of scripts run in the window; in essence, a way to take out the garbage. Nevertheless many people use it for other purposes, the notorious exit pop-up window being a prime offender.