In some scenarios, the web page needs to automatically jump to the specified page after a specified time. For example, if the specified web page cannot be found, the previously set 404 page will be displayed and jump to the specified page. The following is a piece of code that achieves this effect.
The code is as follows:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="https:///" /> <title>jsAfter the specified time, jump to the specified page code instance</title> <script type="text/javascript"> function redirect() { if(second<0) { ='http://wwww.'; } else { if(("Explorer")>-1) { ('totalSecond').innerText=second--; } else { ('totalSecond').textContent=second--; } } } =function() { var second=('totalSecond').textContent; if(("Explorer")>-1) { second=('totalSecond').innerText; } else { second = ('totalSecond').textContent; } setInterval("redirect()",1000); } </script> </head> <body> <h1>The specified page cannot be found</h1> <span >3</span>Automatically jump to the specified page in seconds </body> </html>
The above code can be redirected to the specified page after three seconds. The following briefly introduces the implementation process.
1. Implementation principle:
Use the timer function to modify the numbers in the span element every second. When the number reaches zero, the page will be redirected to the specified link. The principle is roughly this, so I won't introduce it here. For details, please refer to the code comments.
2. Code comments:
redirect(){}, declares an ambiguous, used for jumping.
(second<0) {='http://www.';} , if the number is less than zero, it will jump.
{}, otherwise the countdown effect will be performed.
(("Explorer")>-1), determine whether it is an IE browser.
('totalSecond').innerText=second--, if it is an IE browser, use the innerText attribute to set the numeric value in the span element.
('totalSecond').textContent=second--, other browsers use the textContent property to set the numeric value in the span element.
=function(){}, when the document is fully loaded, then execute the code in the function.
(("Explorer")>-1){}, if it is an IE browser, use the innerText attribute to get the content in the span element.
= ('totalSecond').textContent, other standard browsers use the textContent attribute to get the span element value.
("redirect()", 1000), execute the timer function every second.
3. Related readings:
() function can be found inDetailed explanation of the lastIndexOf() method in JavaScriptOne chapter.
() function can be found inUse and difference between setInterval() and setTimeout()An example chapter.
The above content is the full description shared by the editor of JavaScript based on time to jump to the specified page. I hope you like it.