Use javascript with Cookies to view page 1/3 of History

Source: Internet
Author: User

The previous lack of in-depth understanding of javascript left me helpless.
Later, after the guidance of the two master colleagues (the admiration for these two colleagues is like a continuous stream of rivers and rivers), I suddenly realized that I was suddenly enlightened.
This function is successfully added.

First, we will introduce some objects and methods related to this function in javascript:

1. window. event object:
Event indicates the event status, such as the element that triggers the event object, the location and status of the mouse, and the key to be pressed.
The event object is valid only when an event occurs.

2. event. srcElement:
Indicates the origin of the event, which is generally the place where the event is triggered.

3. srcElement. parentNode:
Indicates the parent node of the event source.

4. srcElement. tagName:
The tag name of the event source.

5. toUpperCase ():
Method for capitalizing the corresponding string

These attributes and methods are basically the same. For friends who are new to javascript or who seldom use such functions before, these objects are a little unfamiliar, but it doesn't matter, it is not difficult to find it after understanding it. It is not much different from javascript verification forms.

Next we will explain the procedures in a step-by-step manner (the procedures are unreasonable and we hope you can correct them and make progress together ):

Part 1: javascript records browsing actions.
Copy codeThe Code is as follows:
Function glog (evt) // defines the function for recording the mouse clicking action.
{
Evt = evt? Evt: window. event; var srcelemem(evt.tar get )? Evt.tar get: evt. srcElement;
Try
{
While (srcElem. parentNode & srcElem! = SrcElem. parentNode)
// Use the preceding statement to determine whether the mouse action is in the valid area and prevent invalid clicks from being recorded.
{
If (srcElem. tagName & srcElem. tagName. toUpperCase () = "A") // you can determine whether the object you click belongs to A link.
{
Linkname = srcElem. innerHTML; // retrieve the name of the event source, that is, the text between <a> and <a/>, that is, the Link name.
Address = srcElem. href + "_ www.achome.cn _"; // retrieve the href value of the event source, that is, the link address.
Wlink = linkname + "+" + address; // integrate the Link name and link address into a variable.
Old_info = getCookie ("history_info"); // retrieves the browsing history recorded previously from Cookies. This function is declared later.
// The following program starts to judge whether the new browsing action is duplicate with the previous six historical ones. If not, the cookies are written.
Var insert = true;
If (old_info = null) // checks whether the cookie is null.
{
Insert = true;
}
Else
{
Var old_link = old_info.split ("_ www.achome.cn _");
For (var j = 0; j <= 5; j ++)
{
If (old_link [j]. indexOf (linkname )! =-1)
Insert = false;
If (old_link [j] = "null ")
Break;
}
}
If (insert)
{
Wlink + = getCookie ("history_info ");
SetCookie ("history_info", wlink); // write cookie, which is declared later
History_show (). reload ();
Break;
}
}
SrcElem = srcElem. parentNode;
}
}
Catch (e ){}
Return true;
}
Document. onclick = glog; // execute the glog function for each page Click action.

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.