tags:

views:

143

answers:

4

I am using jQuery, and wish to make certain links open in a new window.

However, I want to pass as valid XHTML 1.1. Also, I beleive this is a behaviour which should be in the correct layer, i.e. JavaScript.

Any jQuery plugin available?

+1  A: 

I've thrown together a quick jQuery plugin

 (function($){  
  $.fn.newWindow = function(options) {       
    var defaults = {
     titleText: 'Link opens in a new window'  
    };

    var 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 JSON 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: '' } );

This is my first jQuery plugin, so any feedback would be greatful.

alex
A: 

hold on, which XHTML version are you validating against?

the "target" attribute is a valid XHTML 1.0 attribute. Its only not valid in version 1.1.

Could your problem be that you've declared the wrong version at the top of your page?

UPDATE

hmm...well, I don't know much about your app, but if you really need UI which depends on the previous window not being replaced, then maybe you should consider a modal popup.

A link really means, you're going somewhere else. If you need to stay where you are, then why not find another way, such as a modal window, or a hidden/show panel, that falls back gracefully, for further UI?

andy
Sorry, I didn't make it clear. I've edited my question.
alex
@alex: is cool, I suspected that's what you meant, but wasn't sure
andy
Unfortunately, I am forced to do what the client / boss wants. And a new window is what is required.
alex
@alex: haha, yeah, same old story, I completely understand
andy
Probably the same reason why passwords aren't hashed in some scenarios. The boss says: "Wouldn't it be handy if we could email the user their password?"
alex
+1  A: 

All links that start with http:// or ftp:// are externals in my case:

$('a[href^="http"],a[href^="ftp"],').bind('click', function(event){
    open(this.href);
    return false;
});

Or change selector for your purpose.

glavić
This looks like the basis of my plugin. +1 for giving me an answer!
alex
A: 

You can use jquery.url.js

Its pretty easy to use as shown below.

$.jqURL.loc("URL",{w:800,h:450,t:150,l:180,wintype:'_blank'});
Nrj