Showing posts with label javascipt. Show all posts
Showing posts with label javascipt. Show all posts

Tuesday, November 22, 2016

JavaScript - Why to use "var that = this;"

Now a days most of JavaScript files are following Object Oriented Approach.

In simple words, it means it is possible to call super methods using child classes.
So not to lose a reference of function which is calling other one, that is smartly getting used.

Below are good examples explaining about that to refer this:



Keep sharing….!

III
Best Regards,
Abhijeet Rathod.
------------------------------
When 'I' is replaced by 'We',
Even illness becomes wellness.
------------------------------

Sunday, March 17, 2013

Using style.display instead of style.visibility

Hello,

HTML-CSS-JavaScript combination is little bit tricky to non-web developers.

There are few UIs which seems to be inconsistent as we are using JavaScript to enable/disable the HTML Components.
And it takes time to find out and align all the forms.
If you are using any JavaScript and CSS combination to hide/show any components, below information may save your time. 


To maintain proper alignment of whole HTML and its elements, please use following:
Element Property
Value
style.display
'none' (To hide element)
style.display
'' (Empty string to show element)


Please try to avoid using style.display='block' and style.visibility='hidden'/'visible'/'inline' as it is affecting the CSS applied.

---
Cheers and Enjoy Scripting!
-------------------------------------------------------------------------
Alphabet 'O' stands for Opportunity,
Which is absent in 'Yesterday', Is available once in 'Today'
And thrice in 'Tomorrow' So don't think about 'Yesterday'.
-------------------------------------------------------------------------

Tuesday, July 14, 2009

How to call my own JavaScript functions on onLoad or onUnLoad event???

While working on the JSP or HTML pages, we are having some requirement to call our own JavaScript functions after execution of existing onLoad functions.

Also most of the time scenario is like - we are including different files & loading JavaScript files(may be 3rd party). Due to this we are not able to know JavaScript function is getting called on the onLoad or onUnload event for the browser window.

And QUESTION arises : “How to call our own JavaScript functions on onLoad or onUnLoad event?”

Here is the ANSWER for that:

Just add the following JavaScript functions to your own file(JSP/ASP/PHP/JS).

1.       addLoadEvent() : This function will append call of your function to existing queue for onLoad event of browser window.

2.       addUnLoadEvent() : This function will append call of your function to existing queue for onUnload event of browser window.

 

function addLoadEvent(func) {

  var oldonload = window.onload;

  if (typeof window.onload != 'function') {

    window.onload = func;

  } else {

    window.onload = function() {

      if (oldonload) {

        oldonload();

      }

      func();

    }

  }

}

 

function addUnLoadEvent(func){

      var oldOnUnLoad = window.onbeforeunload;

      if (typeof window.onbeforeunload != 'function') {

                window.onbeforeunload = func;

      } else {

      window.onbeforeunload = function() {

      if (oldOnUnLoad) {

            oldOnUnLoad();

            }

            func();

         }

      }

}

 

How to call these functions:

 

In your files make a call to these functions as –

 

addLoadEvent(function(){

                alert(‘Calling myFunction...’);

                myFunction();

                alert(‘ Hurray, myFunction gets called!’);

});

 

addOnLoadEvent(function(){

                alert(‘Calling myFunction...’);

                myFunction();

                alert(‘ Hurray, myFunction gets called!’);

});

 

You can copy & paste these functions to your files & use in the similar way.

Also you can modify the logic of function gets called.