Skip navigation

Tag Archives: Javascript

It is common in forms, to get user inputs for numerical calculations later like house rent allowance for salary calculation. In such scenarios, the value returned is string which if we add result in concatenation. To avoid this mistake adding toInt method to string object directly will be handy.

 * adding toInt method to String object
 * author rajakvk <rajakvk at gmail dot com>
if(!String.toInt) {
   * Returns integer value if a string value is passed.
   * @name toInt
   * @methodOf String
   * @param {*} none 
   * @returns {Number}
  String.prototype.toInt = function() {
     return parseInt(this, 10);
// Usage
// $('#hra').val().toInt();
// typeof $('#hra').val();  // string
// typeof $('#hra').val().toInt();  // number

Note: This method converts string to Integer discarding decimal places.

JavaScript can be loaded dynamically on the fly to improve the performance of the page. Basically it is used as lazy loading without blocking other important content of the page.

Here is the code snippet

var fileref=document.createElement('script')
fileref.setAttribute("src", "myscript.js")
if (typeof fileref!="undefined") document.getElementsByTagName("head")[0].appendChild(fileref)

One issue with this approach is that you can’t see myscript.js in chrome’s sources tab if you want to debug.

Place a comment at the end of the script like below
//@ sourceURL=myscript.js


Hope this helps.

this is powerful but at the same time if used without care it is dangerous too. Consider the example snippet below

// define Human class
var Human = function(name) { = name;
    this.say = function(){
        return "I am " +;
var author = new Human('raja');
var badAuthor = Human('ford');  // new keyword missing

alert(typeof author); // object
alert(typeof badAuthor); // undefined because context changed to window
alert(  // raja
alert(  // ford

Notice how missing new keyword changing the context to window object.

Play with it.