html button jump method

1. Click a button to jump to another page (URL) Two ways of writing:

<button onclick="{location.href='location.html'}">Get current location</button>

<input type="button" value="go" onclick="location.href='http://liujun11.iteye.com">

 Here is the usage of onclick:

onclick="javascript:window.location.href='URL'"

onclick="location='URL'"

onclick="window.location.href='URL?id=11'"

 You can modify the URL when you use it.

2. If you want to pop up a confirmation box and then jump  

Some webpages we have used will definitely prompt you to operate. For example, if you refresh some pages, you will be prompted to confirm whether to refresh. Refreshing may cause data loss.

Add the code directly if(confirm('Are you sure to jump?'))

<button onclick="{ if(confirm('Are you sure?'))location.href='body/location.html'}">Get the current location</button>

<input type="button" value="go" onclick="if(confirm('确定跳转?')){location.href='

http://liujun11.iteye.com'}">

 3. Automatically trigger the click event of the button when the webpage is opened

This only needs to add the attribute onload="abc()" in <body>.

<head>
                        <script type="text/javascript">
                               function abc(){
                                   alert("onload");
                                                    }
                       </script>
                     </head>
                        <body onload="abc()">
                        </body>

 

Here is another point worth mentioning

The difference between button and submit! ! ! !

 type=button is just a button function, if you don't write javascript, nothing will happen when you press it.
 type=submit is to send the form, such a button will automatically submit the form after the user clicks, unless you write javascript to prevent it.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326481177&siteId=291194637