I have used two js files
<script type="text/javascript" src="js/areyousure/areyousure.js"></script>
<script type="text/javascript" src="js/areyousure/ays-beforeunload-shim.js"></script>
<script>
$(document).ready(function(){
$("form").areYouSure();
});
</script>
The above is working only if any of the form input is changed.. I have a dual list box in my page. If I select one of the text and press back button the alert is not working.
Is there any solution for this ?
I have already used onbeforeunload. It did not work. There are some issues with it. Is there any other solution to solve this problem?
The back button is working only if there is change in form. If I have pop ups and pickers it's not working.
You're talking about the browser's back button I suppose.
You should not attempt to listen to the backspace key or anything similar, as there are several ways to leave a page.
This would be the correct way.
window.addEventListener("beforeunload", function (event) {
event.returnValue = "Are you sure?";
});
MDN
Related
I am working on a webpage where a Submit button sumbits a form, but I also want pressing the button to activate a JavaScript function which will disable the Search button and enable a button to reset the search fields and reenable the search button. This is the code currently on the button:
<input id="Search" type="submit" value="Search" onclick="SearchOff()"></input>
And this is the code currently behind that JavaScript function:
function SearchOff() {
document.getElementById("Search").disabled = true;
document.getElementById("Reset_Search").disabled = false;
document.getElementById("[All other relevant fields]").disabled = true;
var x = document.getElementsByTagName("form");
x[0].submit();// Form submission
return true;
}
(I have anonymised the actual function of the application, but it is just entering data into a form which is processed by other JavaScript)
The JavaScript does work, but when onclick="SearchOff()" is in the code, the type=submit function is overridden. Is there any way to get both functions to work? As you can see, I have tried this already with the bottom 2 lines of the JS code, but that was done without onclick="SearchOff()" in the code. I have also tried without this, and had the same problem.
How can I make both functions work at the same time?
Have you tried this idea?
function searchOff(event) {
//No submission yet.
event.preventDefault();
//Disable buttons.
document.querySelector('[type=submit]').disabled = 'disabled';
//Submit the form.
document.querySelector('form').submit();
}
Prevent the form submission when the button is clicked, disable the buttons you need to disable and then, submit the form yourself.
I decided to try retyping the formulas from scratch, which worked onthis occasion for some reason. I think the key was to not copy and paste any of the actual formula, and instead only copy the text to be searched for and counted. This is all I did differently, and I hope reading this helps someone out who reads this.
I need to through alert message like "Are you want to navigate?" when user will modify some data but did not save that and trying to navigate to the other tab.
I want yes or No functionality on clicking on other tab. Not leave this page or stay on this.
Kinldy help me. Thanks In advanced.
function goodbye(e) {
if (!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
window.onbeforeunload = goodbye;
I have used this but it is not showing yes/no.and in same page some buttons are there. After clicking this page is refreshing and this message is coming. can some one plz help me.
You may use beforeunload event.
Your handler should return a string, which will be displayed to a user.
The simplest usage example:
window.onbeforeunload = function() {
return "Do you want to navigate away?";
};
If you have any other questions, refer to the documentation.
If you just want to display some info, then window.alert(Info) could be useful. Since you need to take opinion from user, you can use window.confirm(your Question)
confirm("Do you want to leave this page?");
Confirm is similar to an alert box that popups up.But it has a difference,it gives the user two options: 'Ok' and 'cancel'.
If the user clicks 'ok' button,the window will unload and the user will navigate from the page successfully.
Confirm is often used for verfication and confirmation as the name suggests.
<html>
<body>
<p>Click the button to demonstrate line-breaks in an alert box.</p>
<button onclick="myFunction()">click it</button>
<script>
function myFunction() {
alert("Do you want to navigate away ?");
}
</script>
</body>
</html>
I am trying to briefly disable a 'Save' button on a page during requests to prevent users from clicking it twice. Following advice that I found here, I put
elem.setAttribute("disabled","disabled")
at the very beginning of the onclick method, but it doesn't work, I can still click multiple times very fast and cause multiple requests to be sent before the buttons get disabled. Does anyone have any advice?
Try using the elements properties instead of its attributes
elem.disabled = true;
The onclick method can do this, too. In addition to disabling the button.
if (inclick) return;
inclick = true;
... handle the entire click work ...
inclick = false;
be sure to default inclick = false; at the start of the world.
This will make sure any fast clicks get ignored. (It's a sort of 'debouncing' effect.)
like this
elem.attr("disabled", "disabled");
$("#idButton").attr("disabled", "disabled");
could you help this.
Disabling a submit button after one click
http://jsfiddle.net/V7B3T/12/
I need to invoke an event (for example - display alert) if a user presses on a link (not clicks) - and I don't want it to redirect him to another page. In other words - when a user clicks a link - everything is as usual but when the user presses the link, just the event happens (the alert is displayed) and the user will stay in my page. Is this possible?
By saying press I mean that a user presses on a link for at least one second.
Thanks.
After you refined your question, this code does what you are looking for:
<script>
var timeout;
function onMouseDown(){
timeout = window.setTimeout("alert(1)",1000);
}
function onMouseUp(){
window.clearTimeout(timeout);
}
</script>
click
Notes:
The return false causes the href not to be executed,
The timeout set and clear makes sure that a press of less than a second, won't fire the event
This will fire the alert if the user presses and holds the mouse over 1 second (1000 ms) on the link.
Define press.
You probably want to use either the mousedown or keypress event, depending on what you mean. (The latter case would probably involve checking the event to make sure it was the enter key that was used)
What I understood is that you want:
user pressed the link -> fire an action, but don't redirect the browser
User clicks the link (mouse up over the same object where you pressed the button) -> redirect to a different page
The following code might help
<html>
<head>
<script type="text/javascript">
function mouse_pressed(){
alert('pressed button');
}
function goToLink(url){
window.location = url;
}
</script>
</head>
<body>
<label onclick="goToLink('http://www.google.com');" onmousedown="mouse_pressed();" >text</label>
<body>
</html>
In Jquery you can do this in a simple way.
$("a").live('click',function(){
event.preventDefault();
});
I have a page with a form that is submittes via ajaxSubmit() (so, without changing the page).
My goal is that, when the user try to change page (or even to close the browser), i ask him if really want to exit the page without sending the form (exactly as gmail does).
Gmail for example do this with a window.confirm-like popup, but if it is possible, i'll like to handle it with custom messages and options.
jQuery have the unload event:
$(window).unload( function () { alert("Bye now!"); } );
but it permits me just to do something before exit the page; i need to 'block' the page exit, if the user click the relative button.
So, how to handle (and cancel) the page-exit event?
try the following. Demo here
<script type="text/javascript">
function unloadPage(){
return "dont leave me this way";
}
window.onbeforeunload = unloadPage;
</script>
It's possible bind the "onbeforeunload" event with jQuery:
$(window).bind('beforeunload', function(e) {
return "ATTENZIONE!!";
});
It works!!