But sometimes, when an event is triggered, we need to do some operations first and then jump. At this time, we need to use JAVASCRIPT to implement this jump function.
The specific practices are as follows:
1. Jump to the new page and open it in the new window:
Copy codeThe Code is as follows:
Function gogogo ()
{
// Do someghing here...
Window. open ("test2.html ");
}
Window is a javascript Object. You can use its open method. Note that if the page is not a relative path, add http: //, for example:
Copy codeThe Code is as follows:
Function gogogo ()
{
Window. open ("http://www.google.com ");
}
2: Jump to the page:
Copy codeThe Code is as follows:
Function totest2 ()
{
Window. location. assign ("test2.html ");
}
If you can directly use location. assgin (), but window. location. assign () seems more reasonable. The assign () method of the location object of the current window is used.
In addition, the location object also has a method replace () which can also be used for page Jump. The difference between this method and assign () is as follows:
The replace () method does not generate a new record in the History object. When this method is used, the new URL will overwrite the current record in the History object.
Next we will learn how to transfer the value during page Jump, when using window. when open () opens a new page, the browser will think that there is a relationship between the two windows, so there is a window in the window object of the current window in the new window to be opened. opener attribute. This value contains a reference to open a window. Therefore, you can obtain this value and reference the value of the object on the previous page. The example is as follows:
Copy codeThe Code is as follows:
<Html>
<Head>
<Title> test1 </title>
<Script type = "text/javascript">
Function totest2 ()
{
Window. open ("test2.html ");
}
</Script>
</Head>
<Body>
<Label id = "label1"> page test1 </label>
<Br>
<Input type = "text" id = "tx1">
<Input type = "button" id = "bt2" value = "to test2" onclick = "totest2 ()">
</Body>
</Html>
Copy codeThe Code is as follows:
<Html>
<Head>
<Title> test2 </title>
<Script type = "text/javascript">
Function getvalue ()
{
Var pare = window. opener;
If (pare! = Null)
{
Var what?pare.doc ument. getElementById ("tx1 ");
If (what! = Null)
{
Alert (what. value );
}
}
}
</Script>
</Head>
<Body>
<Label id = "label1"> page test2 </label>
<Br>
<Input type = "button" onclick = "getvalue ()" value = "get test1 page value">
</Body>
</Html>
These two pages can get the values in the previous page from the next page, but I don't seem very practical ......
Advantage: convenient value. As long as window. opener points to the parent window, all objects can be accessed.
Not only can access the value, but also can access the parent window method. The value length is unlimited.
Disadvantage: there must be a relationship between the two windows. It is the window opened by window. open. Cross-Domain is not allowed.
Next let's take a look at another method. We use the URL appended field to pass values between page jumps. This method is used when XMLHttpRequest is used. The following is an example of a simple and original method:
Copy codeThe Code is as follows:
<Html>
<Head>
<Title> test3 </title>
<Script type = "text/javascript">
Function totest2 ()
{
Var parm1 = document. getElementById ("tx1"). value;
Var parm2 = document. getElementById ("tx2"). value;
Var myurl = "test4.html" + "? "+" Parm1 = "+ parm1 +" & parm2 = "+ parm2;
Window. location. assign (myurl );
}
</Script>
</Head>
<Body>
<Label id = "label1"> page test3 </label>
<Br>
<Input type = "text" id = "tx1">
<Input type = "text" id = "tx2">
<Input type = "button" id = "bt2" value = "to test2" onclick = "totest2 ()">
</Body>
</Html>
Copy codeThe Code is as follows:
<Html>
<Head>
<Title> test1 </title>
<Script type = "text/javascript">
Function getparm1 ()
{
Var url = location. href;
Var tmp1 = url. split ("? ") [1];
Var tmp2 = tmp1.split ("&") [0];
Var tmp3 = tmp2.split ("=") [1];
Var parm1 = tmp3;
Alert (parm1 );
}
Function getparm2 ()
{
Var url = location. href;
Var tmp1 = url. split ("? ") [1];
Var tmp2 = tmp1.split ("&") [1];
Var tmp3 = tmp2.split ("=") [1];
Var parm2 = tmp3;
Alert (parm2 );
}
</Script>
</Head>
<Body>
<Label id = "label1"> page test4 </label>
<Br>
<Input type = "button" id = "bt1" value = "get parm1" onclick = "getparm1 ()">
<Br>
<Input type = "button" id = "bt2" value = "get parm1" onclick = "getparm2 ()">
</Body>
</Html>
I remember that when I checked XMLHttpRequest, there was a QueryString object that could be taken directly from the URL parameter. I don't know if it can be used directly here. I tried it and it seems that it won't work.
The last method for transferring values between pages is COOKIE sharing, which is easy to understand. When a page is placed on a client machine and accessed on the next page, directly read the value here and it will be OK.