Examples of six commonly used interaction methods in JavaScript

  • 2020-05-16 06:15:11
  • OfStack

1.confirm message dialog

Grammar: confirm (" str ");

Parameter description: str is the text to be displayed in the dialog box,

Action: usually used to remind the user to make certain choices, the return value is Boolean type, click to confirm the return value is ture, click to cancel the return value is false

Such as:


<script type="text/javascript">
    var mymessage=confirm(" Do you like JavaScript ? ?");
    if(mymessage==true)
    {   document.write(" Very good , Come on !");   }
    else
    {  document.write("JS Powerful, to learn oh !");   }
</script>

2.prompt message dialog

Grammar: prompt (" str1 ", "str2");

Parameter description: str1 is the text to be displayed in the dialog box and cannot be modified; str2 is the content in the text box and can be modified

What it does: a dialog box pops up asking for information about the interaction with the user

Such as:


var myname=prompt(" Please enter your name :");
if(myname!=null)
  {   alert(" hello "+myname); }
else
  {  alert(" hello my friend.");  }

3.window.open opens a new window

Grammar: window. open ('str1','str2','str3');

Parameter description: str1 is the address of the window to be opened; str2 is the name of the window to open; str3 is the parameter of the control window

Effect: opens a new window

Such as:


<script type="text/javascript">
window.open('http://www.imooc.com','_blank','width=300,height=200,menubar=no,toolbar=no, status=no,scrollbars=yes')
</script>

4.window.close close the new window

Grammar: window close (' str '); or < A new window > .close();

Parameter description: str is the address of the open window

Such as:


<script type="text/javascript">
   var mywin=window.open('http://www.imooc.com'); // The newly typed window object is stored in a variable mywin In the
   mywin.close();
</script>

5. alert warned

Grammar: alert (" str "); // note the double quotation marks

Parameter description: str is the popup content of the dialog box

Such as:


<script type="text/javascript"> var mynum = 30; alert("hello!"); alert(mynum); </script>

6.document.write output content

Grammar: document write (" str ")

Effect: output text str

Such as:


</pre><pre name="code" class="javascript"><script type="text/javascript">
  document.write("I love JavaScript ! "); // Content with "" Enclosed, "" The content in the direct output.
</script>

Above 6 points, is the article to share all the content, I hope you can like.


Related articles: