JavaScript FAQ (vii)--dialog box

Source: Internet
Author: User

v. Dialog box

1. Warning (alert)

Q: How do I generate a warning message from JavaScript?

A: To generate a warning message, you can use the alert () method:

Alert (' message goes here ');

The buttons above are created by the following code:

<form name=myform>
<input type=button value= "Try it Now" 
onclick= "alert (' Hello from javascript! ')" >
</form>

2. Confirmation dialog box (Confirm Dialog)

Q: How do I generate a "Confirm/exit" dialog box from JavaScript?

A: To generate the Confirm/Exit dialog box, you can use the Confirm () method:

if (Confirm ("Your question)" { 
 //do things if OK
}

The above button is created by the code:

<form name=myform>
<input type=button value= "Try It Now" " 
onclick=" if (Confirm (' Format the hard disk? '))
alert (' You are very brave! ');
else alert (' A wise decision! ') " >
</form>

3. Prompt Input dialog box (Prompt)

Q: How do I generate a prompt user input dialog box through JavaScript.

A: You can use the prompt () method to generate a dialog box that prompts for user input:

Prompt (' Prompt text ', ' suggested input ')

The buttons above are created by the following code:

<form name=myform>
<input type=button value= "Try It Now" onclick= " 
f=prompt" (' Enter Your name ', ' name ') ;
Alert (' Hello ' +f+ '! ') " >
</form>

4. Exit Prompt Input dialog box (Canceled prompt)

Q: How the user clicks the Exit button, then prompt () will return what value.

a: The return value of the prompt () that was exited is related to the browser. Some browsers return null, while others return an empty string '. Therefore, you can use the following code when calling prompt ():

Userinput = Prompt (' Prompt text ', ' suggested input ');
if (userinput!= ' && userinput!= null) {
 //do something with the input
}

5. Find Dialog dialog box

Q: How to invoke the browser's Lookup dialog box in JavaScript.

a: You can use Window.find () to invoke the Find dialog box in Netscape Navigator 4.0 or in an updated browse. Click to try:

Here the code for the example is:

<form>
<input Type=button value= "find (Netscape only)"
onclick= "if (navigator.appname== ' Netscape ')
&& parseint (navigator.appversion) >3) window.find ();
else alert (' Your browser does not support/' window.find ()/'! ')
" >
</form>

Internet Explorer does not support Window.find (). However, you can use scripts to find text in Internet Explorer and Netscape Navigator, see search text .

6. Bookmarks: Adding to Favorites (Bookmarks:add favorite)

Q: How do I invoke the browser's Add to Favorites dialog box.

a: In Internet Explorer 4.0 or in a newer version of the browser, you can call the browser Add to Favorites dialog box by Window.external.AddFavorite:

Window.external.AddFavorite (' URL ', ' bookmark text ').
Netscape Navigator does not have a similar approach (or, at all, "Add to Favorites"). So your script can only prompt Netscape users to create bookmarks using the Navigator menu or shortcut key (see example below):

example : In Internet Explorer, a link created by a script opens the Add to Favorites dialog box. In Netscape, the script prompts the user to click "Bookmarks" | Add Bookmarkor press ctrl-d to create a bookmark.

The source code for the example is:

if (navigator.appname== "Netscape") {
 document.write (
   ' to-bookmark ' This site, click '
  + ' <b>bookmarks | Add bookmark</b> '
  + ' or press <b>ctrl+d</b>. '
 )
}
else if (parseint (navigator.appversion) >3) {
 document.write ('
  + ' <a onmouseover= ' self.status=/') Bookmark this site/' "'
  + ' onmouseout= ' self.status=/'/'" '
  + ' href= ' javascript:window.external.AddFavorite '
  + ' (/' http://www. javascripter.net/faq//', '
  + '/' javascripter.net faq/') ' > '
  + ' Click here to bookmark this site</a> '
 )
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.