As is known, when a javascript confirmation alert/box pops up, the browser focuses on the box leaving everything else disabled until one of two options are chosen, when "OK" is clicked it returns true, when "Cancel" is clicked it returns false.
Now when writing scripts that involve confirmation alerts, my statements are based on the return of these booleans, now I am wondering (I do not know if this is a dumb question), ist here a way in javascript alone to force or automate the return of either true or false?
For instance, I write a script to detect a confirmation alert, and then force it to return true or false in my script.
Note: I am a mere beginner in javascript, so I do not know if this is a good question or not, but I would appreciate an answer.
Not from within the page.
JavaScript is single threaded and confirm and alert are blocking. No JS will run while they are waiting for input.
If you were, for instance, writing tests for this, then you could try mocking the entire confirm or alert function.
window.confirmValue = true;
window.confirm = function () { return window.confirmValue; }
if (confirm("Hello, world")) {
console.log("True");
}
If you were writing JS from outside the page (e.g. to drive PhantomJS) then the approach might be different.
Related
I have a couple of webpages which i in part build up dynamically (php and JavaScript).
Everything used to work well in the past, but now the various calls to alert and confirm do not cause dialog boxes to appear anymore.
Using the JavaScript debugger in firefox i can verify, for example, that the statement
var r = confirm("Do you really want to save this data?");
is executed, but no dialog window is opened, and the code goes on as if i had pressed "no" (in this step the watch expression for r changes from "undefined" to "false").
The same happens for calls to alert - the code is executed, but no dialog window is shown.
I have not checked these pages in a while, which means there have been various software updates since then.
Strangely however, this seems not to be a browser-wide effect: a different set of similarly built pages does not show this behavior - there, the dialog boxes are shown.
Is it possible that there is some setting in JavaScript or php which prevents dialogs from opening?
Does anybody have an idea how to fix this problem?
I had the same problem before and my problem was solved with the following code. You try it too, maybe it will help you too.
let r = window.confirm("Do you really want to save this data?");
I'm having a really difficult time with this onsubmit. Every time I try to call my function, I get nothing. As soon as I hit 'submit', my form empties and nothing happens. It should be calling a new page to display the information that the user has entered.
I have gone through question after question on stackoverflow and can't find my answer. I'm just learning Javascript and this is probably going to be a very easy question for you all, but since I can't find anything on here that's novice level, I'm stuck referring to you guys for my 'simple' answer. I'm not sure how to submit this question without a huge block of code... so... I apologize for the lengthy code. I did only use one example from the form.
<script>
function ScheduledEvent(evtDate){
evt.evtDate = evtDate;
evt.PrintEvent = PrintEvent
}
function PrintEvent(){
document.write("<p> You have scheduled an event on " + evt.evtDate);
}
function Validate(){
with (document.evtForm){
events = new ScheduledEvent(evtDate.value);
}
with (events){
evt.PrintEvent();
}
return true;
}
</script>
<form name="evtForm" onsubmit="Validate()">
If you need to see more code I'll be happy to oblige. All of the form names and variables match. I cannot figure this out and would love your help.
Well your doing it wrong then. There are a lot of reasons behind this -
Your Validate() method is returning true, which means it will not stop the form submit. So the form will be submitted and the same page will reload. Which explains why it is getting reset. It's happening locally so you are not being able to see the page refresh.
You are calling PrintEvent() before the form is submitted. Thus document.write is actually writing the value in the document but after writing, the page is posted back and reloaded. So you cannot see the updated information.
You are using document.write, which only write on current page, so it will not take you to a different page. And remember, every time a page loads, so does the javascript, which means your javascript objects and states are lost and it is re-initialised.
When javascript writes with document.write it will not clear previously rendered items, that includes HTMLs, the form and whatever you have on that page already rendered. It will just append the line at the bottom of the body tag. To test, return false from validate and you will be able to see the item written.
If you are willing to use a new page, then you have to save the value to somewhere, either on the server or using client local storage or cookie. Try googling about how to do that.
In my application, i need to show the message in alert with OK button.
In that button click event, i want to do some functionalities using the javascript.
So, how can i do this in my app?
Please let me know?
I'm not entirely sure what you are trying to do, but you may be looking for the window.confirm function, which is a built-in function like alert that allows you to capture the response (true or false) from the user:
if(confirm("Some message")) {
//Clicked ok
} else {
//Clicked cancel
}
If you want more functionality than that I'm afraid you'll have to look elsewhere. There are endless modal scripts and libraries available, so just search for one that suits your needs.
Having re-read your question title, maybe you just want the normal window.alert function? That will display a dialog with one button, and, in general, prevent execution of the code following it until the user has closed it:
alert("Some message");
If you'll be using alert() then the execution of javascript will stop where the alert is written. And after the OK button is pressed, the code execution will resume.
So, do an alert(message); and after that line, put the code you want to be executed after the OK button is pressed.
If you want more control, you can use jQuery UI Dialog.
Forget about built-in popups. Even if you make it work you'll probably hit some cross-browser issues and you won't be able to sleep (trust me: I know what I'm saying, I've been there). :)
I've been using jQuery UI ( link ) for some time and I am really satisfied with. Give it a try!
Here's a simple example
if (confirm("Select a button")){
alert("You selected OK");
}else{
alert("You selected Cancel");
}
But if you're just looking for a popup with one button I would go for the alert()
alert("Press OK to close!");
For more popup examples, go to w3schools
I'm trying to validate text and I use an alert() to let the user know when the text isn't valid. My only problem is that, at least with FF4 for me, the alert doesn't do anything to block them from doing things like the back and forward buttons, so if the text isn't valid they can still press back and it will not be validated.
Is there a way to make the alert so that nothing can be done to the browser until the alert is cleared away?
You can bind to window.onbeforeunload and make the user proceed the "correct" way until they get it right.
However, something tells me you need to fix your work-flow and not try to circumvent the user doing what they'd like (programmers always lose).
No, and there shouldn't be. There used to be truly modal alerts, but they were abused like crazy. Same as with no-URL popups and so on. It's a good thing we're rid of them.
In any case, as a web user, I'd expect the back button to always be safe and not leave the application in an inconsistent state. That's probably a better approach
I can't really think of anything that is so important to validate to prevent any other functionaluty and you can never truly achieve this on the client side and if you really have to then you should set a variable on the serverside, in the database for that user lets call it user_preventaction and lets say you set it to TRUE when the page loads that will not change until the input you want is validated and set back to FALSE. Then any page the user tries to access and preventaction is still TRUE it will render a page forcing them to validate whatever you think is so important that the user can even decide to go back.
Even though you can replace the browser's alert box, the functionality is still built-in to the browser, so I don't think you can.
It would be better to consider alternative forms of error display.
I have a web form on my web page. Before the user leaves the page, I want a javascript popup message asking the user if they have completed the form with a "Yes" or "No" button on it. If the user clicks "Yes", then they are brought to the page they intended to go to. However, if the user clicks "No", then the user remains on this page.
I am not very familiar with Javascript so any guidance would be much appreciated. I am suspecting that I would use something like below:
<ELEMENT onbeforeunload = "handler" ... >
Thanks in advance.
Split P.
I'm not sure if this is something you're required to do by someone else, or something you think would be a good idea, but let me say that if you have the choice, please don't do this. If the user wants to leave the page, let them leave the page without telling them something they already know. Further, several modern browsers already have this functionality baked in, so it is just adding another layer of annoying for the user.
Having said that, take a look at this page, which will tell you everything you need to know.
What makes onbeforeunload quirky is that if you return ANYTHING at all in your event handler it will pop up a confirmation alert box asking the user if he or she is REALLY sure they want to leave the page
...
You can insert your own explanatory text BETWEEN those two lines by including a string on the return statement. For instance:
<script type="text/javascript">
window.onbeforeunload = function () {
return "The text you want the alert box to say.";
}
</script>
2 1 thing (thanks #horray I'm helping).
You're better off detecting whether the form has been filled out programmatically, then if the user clicks to continue on without providing a critical piece of data, ask them for it. If they decide not to provide it, let them go.
For example:
var requiredField = document.getElementById("required");
if (requiredField.value == "")
{
if (!confirm("Are you sure you want to go without saving your work?")){
requiredField.focus();
}
}
You could put that in click handlers for links, submit buttons or in the beforeunload event as you mentioned.