jQuery: go to URL with target=“_blank”

后端 未结 7 1967
悲哀的现实
悲哀的现实 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: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;
                }
            }           
        });
    

提交回复
热议问题