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