$(function ($) {
if (window.history && window.history.pushState) {
window.addEventListener("popstate", function (event) {
var hashLocation = location.hash;
var hashSplit = hashLocation.split("#!/");
var hashName = hashSplit[1];
if (hashName !== '') {
var hash = window.location.hash;
if (hash === '') {
var agree = confirm("Are you sure you want to leave?");
if (agree) {
'do something
} else {
'do something
}
}
}
});
window.history.pushState('forward', null, './Payment.aspx');
}
I would like to call the webmethod when user click browser's back button. These code working fine at all other browser except on Safari 5.1.7. The confirmation message should popup when user click browser back button but now popup when page load. Is it possible for me to used the current code to achieve this?
Related
I am using code to logout of application automatically when the browser is closed, but the impact of the code is when I press the browser's back button and navigate to another page then also it automatically logout from the application.
I want my code to logout automatically only when the browser is closed and not when I am navigating through back button
can you help me in this, Thanks
Here is my code
$(window).on('mouseover', (function () {
window.onbeforeunload = null;
}));
$(window).on('mouseout', (function () {
window.onbeforeunload = ConfirmLeave;
}));
function ConfirmLeave() {
$.get("call to a php code to logout");
}
var prevKey="";
$(document).keydown(function (e) {
if (e.key=="F5"){
window.onbeforeunload = ConfirmLeave;
}else if (e.key.toUpperCase() == "W" && prevKey == "CONTROL") {
window.onbeforeunload = ConfirmLeave;
}else if (e.key.toUpperCase() == "R" && prevKey == "CONTROL") {
window.onbeforeunload = ConfirmLeave;
}else if (e.key.toUpperCase() == "F4" && prevKey == "ALT") {
window.onbeforeunload = ConfirmLeave;
}
prevKey = e.key.toUpperCase();
});
The php code is
$this->session->sess_destroy();
$this->load->driver('cache');
$this->cache->clean();
ob_clean();
redirect("login page");
onbeforeunload event occurs when we close the browser and also when we press the back button. It fails to distinguish between the 2 actions.
When a user visits my page and clicks on the back button I want to redirect them to another page. How can I do this? This is in a simple PHP or HTML Web page.
if (window.history && window.history.pushState) {
$(window).on('popstate', function() {
var hashLocation = location.hash;
var hashSplit = hashLocation.split("#!/");
var hashName = hashSplit[1];
if (hashName !== '') {
var hash = window.location.hash;
if (hash === '') {
alert('Back button was pressed.');
window.location='https://www.google.com/';
return false;
}
}
});
window.history.pushState('forward', null, './#forward');
}
This code works on all updated browsers except internet explorer . For internet explorer JSBBQ library needs to be implemented and modified .
function confirmExit(e) {
var f = FormChanges(); //checking whether page has been modified or not
if (f.length > 0){
if (submitForm == false) {
if(!e) var e = window.event;
//e.cancelBubble for IE and it does work
e.cancelBubble = true;
e.returnValue = "You have made updates to this page which have not been saved.";
//e.stopPropagation for Firefox doesn't work.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
}
setTimeout("enableBeforeUnloadHandler()", "100");
}
} //ignore this
window.onbeforeunload=confirmExit;
function enableBeforeUnloadHandler()
{
window.onbeforeunload=confirmExit;
}
When the user wants to go to some other page without submitting the form in the current page,it prompts whether to leave the page or not?
But the problem is, it redirects to the other page in a couple of seconds without waiting for the user action at all,How do i fix this?
(I doesn't work in Firefox,in IE it works fine)
Could be like this:
window.onbeforeunload = function() {
return "Are you sure you want to navigate away?";
}
You actually need to return true (change page) or false (stay on page).
I have a function in JavaScript which executes when the close or refresh button is clicked.
$(window).bind("beforeunload",function(event) {
return "Save or Not";
}
});
This will give me a pop up which has 2 buttons when I click on the refresh button (Reload and Don't reload in Chrome). Now I want to call a function to redirect to my homepage if I click on the reload button. How can I do that?
Please try this
$(document).ready(function() {
setTimeout(function() {
var __redirect_to = 'url_where_to_send';//
var _tags = ['button', 'input', 'a'], _els, _i, _i2;
for(_i in _tags) {
_els = document.getElementsByTagName(_tags[_i]);
for(_i2 in _els) {
if((_tags[_i] == 'input' && _els[_i2].type != 'button' && _els[_i2].type != 'submit' && _els[_i2].type != 'image') || _els[_i2].target == '_blank') continue;
_els[_i2].onclick = function() {window.onbeforeunload = function(){};}
}
}
window.onbeforeunload = function() {
setTimeout(function() {
window.onbeforeunload = function() {};
setTimeout(function() {
document.location.href = __redirect_to;
}, 500);
},5);
return 'Are you sure you want to leave this page?\n\nWait...Before you go:\n\n***************************************\n\n GET A FREE BOTTLE OF NeuroNZT\n\n***************************************\n\nTo receive your bottle FOR FREE - Click Cancel or Stay on Page below';
}
}, 500);
});
</script>
I want to show a <div>that contains loading, using a tag-helper. I want to show the loading anywhere I have a <a> tag, except for href='#'. (I don't have any ajax call, so I will see a new rendered page after clicking the link)
$('a:Not([class="NeedsConfirmation"])').click(function () {
var currentURL = window.location.href;
var element = $(this);
if ((element.attr("href") != "#") && (element.attr("href") != currentURL))
{
showLoading(element);
}
});
Now, I want to add additional exception: currently, If user presses the ctrl and then click the link, the new page will be shown but the previews page will show loading.
How can I discard calling showLoading(element); in this case? (I have no problem with right-click and 'open in new tab' or window)
You can use the event-object passed into .click. It has a property called ctrlKey. Check it and then do the stuff you want.
$('a:not([class="NeedsConfirmation"])').click(function (e) {
e.preventDefault();
// Check if ctrl was pressed...
if(e.ctrlKey) {
// Ctrl was pressed during click
alert('ctrl click');
// Do something else.
}
else {
// Ctrl was not pressed during click
alert('not ctrl click');
var currentURL = window.location.href;
var element = $(this);
if ((element.attr("href") != "#") && (element.attr("href") != currentURL))
{
showLoading(element);
}
}
});
showLoading = function(element){
element.text('loading...');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
click me!
click me with link!