Skip navigation

Category 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+"'])")
    .attr("target","_blank")
    .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
})(true);
//
(function(undefined) {
    alert(typeof undefined);   // undefined
})();
//

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

\\
\\ http://code.jquery.com/jquery-1.7.1.js 
(function( window, undefined ) {

})( window );
\\ http://www.modernizr.com/downloads/modernizr.js
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.

\\ 
jQuery.fn.center = function () {
    this.css("position","absolute");
    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:
$(element).center();
\\

Note: Not tested in all browsers.

Follow

Get every new post delivered to your Inbox.

Join 150 other followers