Baidu Front-end Technical College 2015JavaScript basic part-bom

Source: Internet
Author: User
Tags set cookie setcookie

5.1 Task Description

Implement the following functions

 //  Determine if it is IE, return 1 or version number  function Isie () { //  your  Implement  }  //  Set cookie  function Setcookie (CookieName, Cookievalue, expiredays) { //  your implement  }  //  get cookie value   function GetCookie (cookiename) { //  your Implement } 

1. To determine whether IE browser, you need to take advantage of the only properties of IE, ActiveXObject, before Ie10, directly using the window. Activexobeject to judge, but after the appearance of IE11, there is an exception, in order to be compatible with all IE browser, you can use return (activexobject in window); The most important object in client browser detection is the Navigator object, the Navigator object being one of the earliest implemented BOM objects. It contains some properties of the browser information, such as name, version number, platform. The UserAgent property is a read-only string that declares the value of the user agent header used by the browser for HTTP requests. In general, it is composed of the values of the navigator.appcodename followed by a slash and a navigator.appversion value. For example: mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1;. NET CLR 1.1.4322). The version number we need is the string between the 10th bit and the 11-bit string.

 //  Determine if it is IE, return 1 or version number  //  compatible IE10 and IE11  function Isie () { //  your implement  if  ( " activexobeject    " in   window { return  navigator.userAgent.slice (8 , 11  );  else  { return -1  ; };}

2.cookie is used to save the user's personal information, such as user's user name and login password, can be convenient for users, cookies have multiple parameters, set the name and value of the cookie, must be set only one at a time. The rest is optional and Doucument.cookie can be used to store cookies. The code is implemented as follows:

// Set Cookies function Setcookie (CookieName, Cookievalue, expiredays) {    //  your implement    Document.cookie = cookiename + "=" +cookievalue + "; expires=" + expiredays;}

3. Get the name of the cookie to get the cookie value, you can first convert the string of information stored in the cookie into an array so that each pair of key values is put together to facilitate the second operation. Then the array is delimited, and if the first value of the array is found to be the same as the value we want to get, then the second element of the arrays is the value we want. The code is as follows:

// Get Cookie value function GetCookie (cookiename) {    //  your implement    var cookie = Document.cookie.split (";" );    Each (cookie,function(item,index) {        if (Trim (item.split ("=") [0] = =  CookieName) {            return item.split ("=") [1];        }}    )}

Reference: http://www.itxueyuan.org/view/6375.html

Baidu Front-end Technical College 2015JavaScript basic part-bom

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.