How to implement automatic page jump of website using PHP and JSP (Part 2)_javascript skills
WBOY
Release: 2016-05-16 18:22:14
Original
940 people have browsed it
This article discusses several implementation methods of automatic web page jump. Method 1: Use meta tag The meta tag is one of the indispensable tags in HTML. It is responsible for providing meta-information of the document. Its main parameters are: ① http-equiv: HTTP file related to the data in the document Header ② content: Data related to the named HTTP header ③ name: Document description ④ url: URL associated with meta information When we define the attribute http-equiv as refresh, when we open this Web page The system will jump to the corresponding page within a certain period of time based on the value specified by content. content="seconds;url=website" defines how long it takes to jump to the specified URL. The following meta tag tells the system to automatically jump to the page after one second: The above code needs to be added to the header of the HTTP document, between
and . Usually, the meta tag follows . If multiple meta tags are required, they can each occupy their own line. This method is applicable to any environment, including static website space. Method 2: Use the header function The header function is one of the HTTP-related functions in PHP's built-in functions. This function sends the HTTP protocol header to the browser. Use it to redirect the URL, that is, the page will redirect to other specified web pages. The following example will automatically open the Black Horse Online Power homepage after execution: header("Location: http://www.jb51.net/"); It must be noted that the header function can only be used in the page code. Before the tag, that is, before any other headers () in the HTTP header have been sent to the browser, and the previous page cannot print or echo any content. In other words, before the of the page appears, the program simply handles the header event. Despite such strict requirements, if you use it flexibly, you can still achieve the automatic jump function of the page, such as the login page, and decide where to jump to the page by judging whether the data submitted by the user is legal. A simple example is given below:
/* Login program - File name: login.php Program function - Determine user login password*/ if($_POST['Submit']) { session_start(); if($_POST['pws']=='123') { //If the password is 123 $_SESSION['passwd']='123'; //Write session data header("Location :index.php"); //Jump to the normal page }else{ header("Location:login.php"); //Jump to the login page } } //The form code is abbreviated (the form can also be written in pure html code, if so, the code should be placed after the program ?> /* Detect session data - file name :index.php Program function - Check whether the password in the session data is 123, if not, return to the login page*/ session_start(); if($_SESSION['passwd'] !='123') header("Location:login.php"); //Other code (pure HTML code should be written after the program) ?>
This This method can obviously only be used in space environments that support PHP. Method 3: Using JavaScriptJS is very flexible and can be used to create very powerful program scripts. Here is a simple JS example of automatic page jump. After the code is executed, the browser will automatically go to the Black Horse Online Power website. The code can be placed at any legal location on the page: This code is suitable for any Web environment. If you add a timer, it will be even more wonderful.
Frequently encountered pages. For example, some websites need to display a prompt page when logging out. What you are logging out is just polite, and some even need to make a countdown. Method 1: < meta http-equiv="refresh" content="'Waiting time';URL='Jump page'"> Method 2: This is a js application. Test code:
< script> t = -1; // Counter setInterval("testTime()",1000); // Start 1 second timer function testTime() { if(t<0) return; // The counter value is less than 0, indicating that the countdown has not started yet if(t == 0) // The counter value is 0, redirect to location = "http://www.jb51.net"; view.innerHTML = "" t ""; // Display countdown t--; // Decrement the counter } function offTime() { if(event.srcElement.value != "Click to start") { // If the countdown does not start t = -1; // Initial counter view.innerHTML = ""; // Clear the countdown } } document.onclick = offTime; // Start click event monitoring
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn