For developing and enhancing your websites or blogs, JavaScript is essential in some sense. Here, I list simple but useful and essential functions of JavaScript. You may want to seek jquery functions or methods for solving the problem, but it may make your pages heavy sometimes, especially in the case of JavaScript is capable to do that simply. (I do not say jquery is a heavy stuff. I love jquery!)
1. Getting a URL Parameter By Name
This function returns URL parameter by name so that a developer controls user requests by parameter in Client's Browser.
2. Replacing All Occurrences of a String to Another String
JavaScript function repalce() replaces one time by default. For example, str.replace('a', 'b') where str = ababa returns bbaba. You can replace all occurences by using regular expression. In facts, many versions of replace functions are introduced, but next function is the fastest one cross browsers. See this test. Note that replaceAll() function in jquery is not for String object.
3. Checking a String Starts or Ends with another String
startsWith() and endsWith() functions ( which Java has) may very useful to you if you have many String processing.
4.Checking an Array has a value (item)
This function returns true if an array has an item, otherwise return false. Add to Array Prototype. You can just define the function like startsWith function.
1. Getting a URL Parameter By Name
This function returns URL parameter by name so that a developer controls user requests by parameter in Client's Browser.
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}
2. Replacing All Occurrences of a String to Another String
JavaScript function repalce() replaces one time by default. For example, str.replace('a', 'b') where str = ababa returns bbaba. You can replace all occurences by using regular expression. In facts, many versions of replace functions are introduced, but next function is the fastest one cross browsers. See this test. Note that replaceAll() function in jquery is not for String object.
str.replace(/old_string/g,'new_string');
Example. Replace all dots to semicolons
mystring.replace(/\./g,';')
3. Checking a String Starts or Ends with another String
startsWith() and endsWith() functions ( which Java has) may very useful to you if you have many String processing.
function startsWith(str){
return this.slice(0, str.length) == str;
}
function endsWith(str){
return this.slice(-str.length) == str;
}
If you use the functions frequently or make it simple, just add these functions to the String Prototype.
if (typeof String.prototype.startsWith != 'function') {
String.prototype.startsWith = function (str){
return this.slice(0, str.length) == str;
};
}
if (typeof String.prototype.endsWith != 'function') {
String.prototype.endsWith = function (str){
return this.slice(-str.length) == str;
};
}
This function returns true if an array has an item, otherwise return false. Add to Array Prototype. You can just define the function like startsWith function.
Array.prototype.has = function(v) {
for (var i = 0; i < this.length; i++)
if (this[i] === v) return true;
return false;
};
Thanks for sharing this good blog.It's amazing blog
ReplyDeleteJava Online Training