jQuery: go to URL with target=“_blank”

后端 未结 7 1943
悲哀的现实
悲哀的现实 2020-12-04 15:12

I am using this bit of jQuery code to get href of the link:

var url = $(this).attr(\'href\');

-- and this bit of code to go to that href:

相关标签:
7条回答
  • 2020-12-04 15:46

    You need to open a new window:

    window.open(url);
    

    https://developer.mozilla.org/en-US/docs/DOM/window.open

    0 讨论(0)
  • 2020-12-04 15:52

    Try using the following code.

    $(document).ready(function(){
        $("a[@href^='http']").attr('target','_blank');
    });
    
    0 讨论(0)
  • 2020-12-04 15:55

    Detect if a target attribute was used and contains "_blank". For mobile devices that don't like "_blank", this is a reliable alternative.

        $('.someSelector').bind('touchend click', function() {
    
            var url = $('a', this).prop('href');
            var target = $('a', this).prop('target');
    
            if(url) {
                // # open in new window if "_blank" used
                if(target == '_blank') { 
                    window.open(url, target);
                } else {
                    window.location = url;
                }
            }           
        });
    
    0 讨论(0)
  • 2020-12-04 15:56

    The .ready function is used to insert the attribute once the page has finished loading.

    $(document).ready(function() {
         $("class name or id a.your class name").attr({"target" : "_blank"})
    })
    
    0 讨论(0)
  • 2020-12-04 16:00

    If you want to create the popup window through jQuery then you'll need to use a plugin. This one seems like it will do what you want:

    http://rip747.github.com/popupwindow/

    Alternately, you can always use JavaScript's window.open function.

    Note that with either approach, the new window must be opened in response to user input/action (so for instance, a click on a link or button). Otherwise the browser's popup blocker will just block the popup.

    0 讨论(0)
  • 2020-12-04 16:01

    Use,

    var url = $(this).attr('href');
    window.open(url, '_blank');
    

    Update:the href is better off being retrieved with prop since it will return the full url and it's slightly faster.

    var url = $(this).prop('href');
    
    0 讨论(0)
提交回复
热议问题