JavaScript automatically jumps to a page after x seconds

Source: Internet
Author: User

Today, I learned a new technology, that is, when I click "Submit" or "Confirm" on a page, it will automatically jump to a page.
I searched the internet and found that there are many ways to handle this technology. I only wrote down three of the methods I learned in the video:
1. Use response. sendRedirect ("target page. jsp \. htm;
2. Sometimes we need a prompt, such as "automatically jump after x seconds. If no jump is made, click here". In myeclipse, you can call Delay Go To URL in Snippets. the following code is automatically generated:
Copy codeThe Code is as follows:
<Script language = "JavaScript1.2" type = "text/javascript">
<! --
// Place this in the 'head' section of your page.
Function delayURL (url, time ){
SetTimeout ("top. location. href = '" + url + "'", time );
}
// -->
</Script>
<! -- Place this in the 'body' section -->
<A href = "javascript:" onClick = "delayURL('myPage.html ', '000000')"> My Delayed Link </a>

Modify this code:
Copy codeThe Code is as follows:
<Script language = "JavaScript1.2" type = "text/javascript">
Function delayURL (url, time ){
SetTimeout ("top. location. href = '" + url + "'", time );
}
</Script>
<Span id = "time" style = "background: red"> 3 </span>
Automatic jump in seconds. If no jump is made, click the following link.
<A href = "target page. jsp"> Target page </a>
<Script type = "text/javascript">
DelayURL ("http://www.hualai.net.cn", 3000 );
</Script>

Then, the system will jump to the "target page" in 3 seconds ". In this way, the page will not change when the jump is set for a few seconds, for example, set 3 seconds, and then change 3 to 2 as time changes, and then change to 1 until the jump is made, see the third method below.
3. modify the code in method 2:
Copy codeThe Code is as follows:
<Script language = "JavaScript1.2" type = "text/javascript">
Function delayURL (url ){
Var delay = document. getElementById ("time"). innerHTML;
// The final innerHTML cannot be lost; otherwise, delay is an object.
If (delay> 0 ){
Delay --;
Document. getElementById ("time"). innerHTML = delay;
} Else {
Window. top. location. href = url;
}
SetTimeout ("delayURL ('" + url + "')", 1000 );
// 1000 milliseconds, that is, jump every second
}
</Script>
<Span id = "time" style = "background: red"> 3 </span>
Automatic jump in seconds. If no jump is made, click the following link.
<A href = "target page. jsp"> topic list </a>
<Script type = "text/javascript">
DelayURL ("http://www.hualai.net.cn/news/knowledge/265.html ");
</Script>

The effect of this method is to jump to the target page after clicking submit on the previous page and jump to this page after 3 seconds (this 3 will be reduced to 0.

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.