Skip to main content

Simple But Essential Functions of Javascripts

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.
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;
};
}
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.
Array.prototype.has = function(v) {
for (var i = 0; i < this.length; i++)
if (this[i] === v) return true;
return false;
};

Comments

Post a Comment

Popular posts from this blog

Google's Favorite Colors By Hex Codes (With New Google Logo)

Google keeps its color scheme cross their products. Of course, each uses different colors but it keeps the main philosophy. I heard that the test period for new Google logo was more than one year. It may the reason that the colors are comfortable to look. Sometimes, I copy the colors for designing my blogs and web sites. Copyright? oh..

Color Conversion: RGB to HEX , HEX to RGB

Color conversion between RGB and Hex is needed sometimes even though HTML and CSS accepts both RGB and HEX codes for set color scheme of HTML elements. The conversion algorithm is simple. Then numbers in RGB format is decimal number and one in HEX code is hexadecimal number. For example, rgb(50, 100, 250) is same in HEX #3264FA: 50-32 | 100-64 | 250-FA.

Why Blogger Mobile Template Not Work?

Google Blogger shows blog pages with mobile CSS on mobile devices unless you change it disabled. You can see next figure by clicking mobile setting button on Template page. Since I want to change CSS and I remember there are some CSS elements starting with '.mobile', I open the template editor and edit as I want. And open my blog in my smart phone. Nothing changed.