Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

press on any key will close alert

Status
Not open for further replies.

mici

Programmer
Jun 2, 2003
36
0
0
IL
I have an alert message
alert("hello");

I want that instead of the user clicking the ok button,
he will be able to press any keyboard and the alert will be goen.

How do i do it?
 
I could be wrong but I do not think this is possible with an alert box. It is a pre-defined function in javascript and I do not think you can modify it's controls.

What you could do instead of an alert is to display a positioned DIV on the page with your message and trap for key presses while it is displaying. You would have much more control over the style of the box and it's controls doing it this way.


At my age I still learn something new every day, but I forget two others.
 
Look at this link for an alternative type of alert box. It does contain the code to trap any keypress but that can be added in if this approach is acceptible.


At my age I still learn something new every day, but I forget two others.
 
Thank you ! I will try that ,
can you tell me also please -How do I trap any keypress
to close this message?

Thanks
 
You could set an onkeypress event in the document body tag like:
Code:
<body onkeypress="keyPressed();">

Now the trick is, you need to know when the alert message is displayed on the screen. So you have a variable declared globally:
var alrtDisp = false; //Declare this globally outside of function.
Then when your function displays the div with the message you can set the flag like:
alrtDisp = true;

Then the function called from the onkeypress event has to check that flag to see if it is true or false and ignore the keypress if it was false.
Code:
function keyPressed() {
  if (alrtDisp)
    //Alert is being displayed and key pressed
    //so hide the alert div and then reset the alrtDisp
    //value to false.
}

At my age I still learn something new every day, but I forget two others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top