Web page Jump implemented by JavaScript and cross-page value transfer method

Source: Internet
Author: User

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: CopyCode The 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 code The 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 code The 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 code The 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 the Code the code is as follows:

test1



page test4



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.

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.