View Full Version : Detect an alert...
pitchoo
06-25-2002, 03:38 PM
Is it possible to detect if an alert box is opened?
JohnKrutsch
06-25-2002, 05:20 PM
Did you open one? If an alert box is open, your code will wait until it is closed before it does anything else. Explain more of what it is you are trying to accomplish.
pitchoo
06-25-2002, 05:32 PM
Well your right... wow I'm tired!
I have an onchange event which contains a function that check the syntax.
Then another function is added dynamically to this event to make a dynamic calculation.
So if a user enter a wrong type of value the alert appears but when he click "OK", the second function is called and I don't want that. So how can I do that?
In a simple case, I would have tested if the first function returns true and then call my second function but it's a little more complicated here. A solution should be to dynamically modify the first function to make a test on it but this seems complicated....
For the moment I don't even know how to add dynamically a second function to my event so (problem posted today)....
HELP!!!
Graeme Hackston
06-25-2002, 10:44 PM
In the open code before and outside of your functions put
MyCorrectValue = false;
In the function where they have entered the correct value put
MyCorrectValue = true;
In the function where you put the alert put
MyCorrectValue = false;
In the function where the user clicks ok put
if (MyCorrectValue) { // This means the variable is true
call the 2nd function;
} else {
} // This means do nothing
Graeme Hackston
06-25-2002, 10:49 PM
The above way aparently throws a warning in Mozilla. Don't hold me to this but I believe it's the old way of doing it. I think you need to decide between code bloat and catering to (for now) Mozilla.
This method was given to me in the old forum by another member. It keeps the variables inside the functions. Here is 2 examples.
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="JavaScript">
function first() {
alert('first running');
first.done = true;
}
function second() {
if (typeof first.done == 'undefined') return;
alert('second running');
}
</script>
</head>
<body>
<a href="javascript: void first()">Run First Function</a><br>
<a href="javascript: void second()">Run Second Function</a>
</body>
</html>
<html>
<head>
<title></title>
<script type="text/javascript" language="JavaScript">
function first() {
//set property if calling this for first time, otherwise leave alone
if (typeof first.msg == 'undefined') first.msg = 'first running';
alert(first.msg);
second.enabled = true;
}
function second() {
if (typeof second.enabled == 'undefined') second.enabled = false;
if (!second.enabled) return;
alert('second running');
}
function enableDisable() {
//if defined & true set to false otherwise do both
second.enabled = (typeof second.enabled != 'undefined' && second.enabled == true) ?
false : true;
}
function modify() {
if (typeof first.msg == 'undefined') first.msg = 'first running';
first.msg = prompt('Message in first()?', first.msg);
}
</script>
</head>
<body>
<a href="javascript: void first()">Run First Function/Enable Second Function</a><br>
<a href="javascript: void modify()">Modify First() message</a><br><br><br>
<a href="javascript: void second()">Run Second Function (only after first)</a><br><br>
<a href="javascript: void enableDisable()">Disable/Enable Second Function</a>
</body>
</html>
pitchoo
06-26-2002, 01:32 PM
Ok it works good but in my case it's a little bit more complicated:
I'll explain an example of the behaviour I want to implement:
I have a form with 3 fields:
field A : must contain an integer
field B: must contain an integer
field C: is the result of a predefined formula, let's say A + B
If I enter a new value in field A, the result in field C is dynamically updated.
If I enter a string in field A, an alert shows up and field C is not updated.
That works, but if I enter a string in field B and then a new integer in field A, the sum function is enabled (but shouldn't!) and logically raises an error because field B is not an integer... So how can I solve this problem???
HELP!
Graeme Hackston
06-26-2002, 10:37 PM
Sorry I'm not familiar with forms, you can't pick them up with variables?
You can do things like
if ((fieldA) && (fieldB) && (fieldC==false)) {
do stuff;
} else {
if ((fieldA) == (fieldC)) {
other stuff;
}
}
I could be wrong on the syntax (maybe a semi-colon after false?) but there are lots of options.
Graeme Hackston
06-26-2002, 10:41 PM
Here's a good tutorial I just came across
http://www.it.maconstate.edu/tutorials/default.htm
adios
06-26-2002, 11:25 PM
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="JavaScript">
function update(field) {
if (!/^-?\+?\d+$/.test(field.value)) {
alert('Please enter an integer value.');
field.focus();
field.select();
} else {
var temp = 0;
with (field.form) {
if (!isNaN(Number(field_A.value))) temp += Number(field_A.value);
if (!isNaN(Number(field_B.value))) temp += Number(field_B.value);
field_C.value = ' ' + temp;
}
}
}
</script>
</head>
<body>
<form>
field A <input type="text" name="field_A" onchange="update(this)"><br>
field B <input type="text" name="field_B" onchange="update(this)"><br>
field C <input type="text" name="field_C" readonly="readonly">
</form>
</body>
</html>
I think you're saying 'string' when you mean 'digits'; all form data is stored as strings.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.