What's the best way to open new browser window?

前端 未结 10 1316
一生所求
一生所求 2021-01-30 03:06

I know that most links should be left up to the end-user to decide how to open, but we can\'t deny that there are times you almost \'have to\' force into a new window (for examp

10条回答
  •  梦如初夏
    2021-01-30 04:02

    Here is a plugin I wrote for jQuery

     (function($){  
      $.fn.newWindow = function(options) {       
        var defaults = {
            titleText: 'Link opens in a new window'     
        };
    
         options = $.extend(defaults, options);
    
         return this.each(function() {  
           var obj = $(this);
    
           if (options.titleText) {        
               if (obj.attr('title')) {
                         var newTitle = obj.attr('title') + ' (' 
                                                    + options.titleText + ')';
               } else {
                        var newTitle = options.titleText;
               };              
               obj.attr('title', newTitle);            
           };          
    
           obj.click(function(event) {
              event.preventDefault();  
              var newBlankWindow = window.open(obj.attr('href'), '_blank');
              newBlankWindow.focus();
            });     
           });    
      };  
     })(jQuery); 
    

    Example Usage

    $('a[rel=external]').newWindow();
    

    You can also change, or remove the title text, by passing in some options

    Example to change title text:

    $('a[rel=external]').newWindow( { titleText: 'This is a new window link!' } );
    

    Example to remove it alltogether

    $('a[rel=external]').newWindow( { titleText: '' } );
    

提交回复
热议问题