Thor Aniket Thor Aniket - 1 month ago 8
jQuery Question

Write a jquery plugin to open a link in new window

I have wrote this plugin to open link to new window but does not work
any idea what might have gone wrong?

(function( $ ) {
$.fn.myPlugin = function() {
var defaults = {
width: 800,
height: 700,
scrollbars: 1,
location: 1,
status: 1
},
self = this,
opts = $.extend(defaults, options);
this.filter('a').click(function() {
$("a").attr("target","_self");
window.open($(this).attr('href'),'title', opts);
return this;
});
};

}( jQuery ));
$('a').myPlugin();

Answer

Instead this

   opts = $.extend(defaults, options);
   this.filter('a').click(function() {
        $("a").attr("target","_self");
        window.open($(this).attr('href'),'title', opts);
        return this;
    });

try:

opts = $.extend({}, defaults, options);

$('a').click(function(){
   window.open($(this).attr('href'),'title', opts);
});

Here you can find examples of using window.open(). Also read doc:

Keep in mind that the target object (first argument) will be modified, and will also be returned from $.extend(). If, however, you want to preserve both of the original objects, you can do so by passing an empty object as the target:

var object = $.extend({}, object1, object2);

Hope this help you.

Comments