Assign a value on OK button of JavaScript Alert Messagebox - javascript

I know that confirm message-box of JavaScript can be assigned a value. Now, i also want an alert message-box (OK button) to have a value, So that i can use it in my server-side code in ASP.Net.
Is this possible?

It seems you haven't understood well about the alertbox.
In any javascript function if alert message is shown then after you click the ok button only next statement will get executed.
But if u really want to assign a value to ok button then just declare a variable and assign a value to that particular variable in your code after the alert code. Sample code will look like below.
function fnMyFunction(){
var myval="";
//your code goes here
alert('Your Message');
myval="your value";
}

Related

Customize Javascript Alert NOT onclick

I have absolutely no idea about Javascript and don't know if it is even possible. Anyway, I found a script that customizes Javascript Alert Box. It works nicely. The issue I am having is that this customization works based on 'onClick' as below:
// Standard Dialogs
$("#alert").on( 'click', function () {
reset();
alertify.alert("This is an alert dialog");
return false;
});
However, my Alert Box is generated by different code, and it either generates Alert Box or redirects to another page, but has nothing to do with onClick function. My question is:
is it possible to customize the Alert Box which is generated outside of the onClick function? If so, could anyone let me know how?
And my sincere apologies if that subject was already explained somewhere - I was looking for an answer but could not find.
Thank you
If you're trying to have the alert pop up initially without prompt it appears you will need:
alertify.alert("This is an alert dialog");
to be pasted outside of the function.
This will trigger the alert when the page loads as long as your tags are at the bottom of your html &/or you've used a document.ready statement.
My other interpretation of your above message is that the code above works and another alert box doesn't in which case can you please show us the code from the code that is not displaying as customised.

window.onbeforeunload returns user to same page

On leaving a page with a form, I want to save the changes made without any user confirmation.
Having found this question I adapted one of the answers as follows. Note that I am not using return:
function setConfirmUnload(on) {
window.onbeforeunload = (on) ? unloadMessage : null;
}
function unloadMessage() {
alert('Gonna save now');
setTimeout(validate_submit,500);
}
This is only triggered when the user fills some value in the form. If an element value changes, I use
setConfirmUnload(true);
Result in FF23: when user clicks somewhere to leave the page, the alert is shown, validate_submit() is executed on "OK", new page appears - BUT now the alert reappears and the user is returned to the original page on "OK". Why? Can somebody confirm this behaviour?
The window.onbeforeunload method causes a confirm box to appear containing whatever message is returned by the function assigned to it. You can put alerts and other stuff in this method, but it will always end with a confirm message. If your function returns nothing, it will still give a message (determined by browser) saying "are you sure you want to leave this page" or something along those lines.

Refresh the page after click OK on alert box

In checkout page of my magento store, I need to clear some inputs when an alert box is showed and user press OK. Is possible to do that?
I have no control over the javascript alert. So I think in a script that detect an alert with a specific message and clear inputs when button of that alert is clicked.
UPDATE
Fixed!
file: opcheckout.js
line: 888
I add location.reload(); because document.location.reload(true); not work on IE. Thanks everybody!
Probably try overriding default alert and write a custom function like below,
var c_alert = alert;
window.alert = function (str) { //override default alert
c_alert(str + ' my message');
location.reload();
//or write code to clear input fields here
}
//below seems to be triggered from somewhere where you don't have control.
alert('test');
Javascript
if(confirm('your confirmation text')){
document.location.reload(true);
}
Make it refresh after the alert. The javascript code shouldn't execute before the alert is gone

oncomplete method doesn't always executes after action, JSF

I have the next scenario (simplified) :
My function in my docsMB managed bean:
public void saveAdvance() {
// by default, finalized is false
finalized = getFinalizedByBySlowFunction();
}
A button in my xhtml:
<a4j:commandButton value="SAVE"
action="#{docsMB.saveAdvance}"
oncomplete="verifyDocs();"/>
And my verifyDocs function in the same xhtml:
<script type="text/javascript">
var verifyDocs = function(){
//alert(1);
if( #{docsMB.finalized == true} ){
#{rich:component('mpConfirmar')}.show();
}
}
</script>
This make me a problem.
If I execute my app, "finalized" is always false, but I realized that if I uncomment the alert in the verifyDocs function, it works.
I think the alert gives the "necesary" time to saveAdvance to finalize itself.
What can I do to be sure that the oncomplete method excecutes after the action has finalized?
Or am I doing a mistake in other place?
Thanks a lot
[Edit]
If I refresh the entire page, I get the correct "finalized" value...
The problem was that when I define the javascript function, it became like static, it means, It just execute once and the variable finalized gets the defined values when the page loads.
So, when I clicked the button it retrieve the value predefined, no te value at that moment.
But if I refresh the page, the variable gets the value at the moment of the refresh.
So to solve this, I just put my javascript code directly in the oncomplete method and finally WORKS :D
Maybe a better solution could be to find a way to refresh the javascript code each time the button is clicked, because if I put the javascript code in the oncomplete method, it seems dirty, because of the lot of characters in a line.

How to block alert box through jQuery

I want to block alert box if it is present in code. Im using an api that tells me the search result of my website and if any user enter
<script>alert('Just teasing')</script>
then it shows an alert box on my page how can i stop this alert?
First of all you should sanitize you input as #Nikita commented.
If you want to accept JavaScript and only disable alert you can replace the window.alert function.
window.alert = function() { /* do nothing here */ }
Now calling alert won't do anything.
When presenting the search results back to the user you need to ensure you HTML encode the output so the user would see the script rather than it being executed.

Categories

Resources