Skip navigation

Tag Archives: jQuery

It is good practice to warn users while navigating away from one domain to another. How to achieve this using jQuery?

The code snippet below demonstrate this

$("a[href^='http://']:not([href*='"+location.hostname+"']), [href^='https://']:not([href*='"+location.hostname+"'])")
    .click(function(e) {
        alert('You are leaving this website and going somewhere else');

Hope this helps!!

Credit goes to Alex Staveley and open web.


Learning experience with JavaScript is more excited day-by-day.

(function(undefined) {
    alert(typeof undefined);   // boolean
(function(undefined) {
    alert(typeof undefined);   // undefined

Refer jQuery source code (development version 1.7.1) and modernizr, the first line would be

(function( window, undefined ) {

})( window );
window.Modernizr = (function( window, document, undefined ) {

})(this, this.document);

To overcome the issue of undefined, these libraries passing undefined as local variable. Notice the third parameter is left blank.

Read my another post regarding the same topic.

You can see the power of jQuery plugin below. The following code will extend the jQuery $ and center whatever element you are selecting.

\\ = function () {
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
//Use the above function as:

Note: Not tested in all browsers.