Share several JavaScript-related face questions.
String inversion
Here are two ways to solve a problem. If you have other ideas, you can share the communication!
The first method:
function reverse (str) { var sp = str.split (', '); Save the reversed string var new_str = '; /* from back to forward, separated by commas * /for (var i = sp.length-1;i>=0;i--) { New_str + = sp[i]+ ', '; } /* Fetch to LENGTH-1, otherwise the last comma will be taken. * /New_str = new_str.substring (0,new_str.length-1); return NEW_STR;}
The second method:
function reverse (str) { return str.split (', '). Reverse (). Join (', ');}
Write a JavaScript function parsequerystring, which is used to parse URL parameters into an object, such as: var url = "Http://witmax.cn/index.php?key0=0&key1=1 &key2=2″
1. Use? Split the URL into two parts, the first part: Http://witmax.cn/index.php, the second part: key0=0&key1=1&key2=2. That is, the address and parameters are separated and stored in an array.
var arr = url.split ('? ');
2. On the basis of 1, the previous part, that is, the parameter part is used & split into multiple parts, such as Key0=0,key1=1. Then the key value pair is opened with the = sign.
var arr = arr[1].split ("&"); for (var i = 0; i < arr.length; i++) { var a = arr[i].split (' = '); Params[a[0]] = a[1];}
Full code
function parsequerystring (URL) { //If the URL is empty or the URL has no parameters then return directly if (url = = = NULL | | url.indexof ('? ') < 0) return; var params = []; Split the URL into 2-part var arr = url.split ('? '); if (arr.length <= 1) { return params; } With & split into multiple parts, each part is a key value pair //and then use = to split the key value pair arr = arr[1].split (' & '); for (var i = 0; i < arr.length; i++) { var a = arr[i].split (' = '); Params[a[0]] = ar[1]; } return params;}
Click any element on the page to eject the tag name of the element
Browser compatibility issues are covered here. Simply describe the difference between target and srcelement and event.
in Internet Explorer, the event object exists as a window.event global object. In Firefox, this is the event parameter .
Srcelement is used in IE, target is non-ie browser usage. For compatibility, it can be obtained in the following ways:
target = event.srcelement? Event.srcElement:event.target;
To get the label name you can pass the TagName property.
function Gettarget (event) { event = Window.event | | event; return Event.srcelement | | Event.target;} Document.onclick = function (event) { var tag = Gettarget (event); alert (tag.tagname);}
Write a method that asks for the byte length of a string
Suppose that an English character occupies one byte, and a Chinese character occupies two bytes.
The main method used is charCodeAt (). The charCodeAt () method returns the Unicode code for the specified position character, and the encoded value greater than 255 is a non-regular character, such as Chinese, English, and so on.
function GetBytes (str) { //No reference if (arguments.length = = = 0 | |!str) return NULL; if (str.length <= 0 | | str = = = = ") return 0; var len = str.length; Number of bytes var bytes = 0; for (var i = 0; i < len; i++) { if (str.charcodeat (i) > 255) { bytes + = 2; } else{ bytes + = 1; } } return bytes;} Console.log (GetBytes (' Code of Beauty, beautiful Codes ');//24
A brief analysis of JavaScript face test