1

I use this script to find link around and open it on click. But it always open it in same tab. How can I adjust it to open found link in new tab/window?

<script>
    
    (function ($) {
    $(document).ready(function () {
    $('.my-clickable-blank').click(function() {
    window.location = $(this).find('a').attr('href'); 
    return false;
    });
    });
    })(jQuery);

</script>
Ozgur Sar
  • 2,067
  • 2
  • 11
  • 23
Michal
  • 11
  • 5
  • 2
    Does this answer your question? [javascript window.location in new tab](https://stackoverflow.com/questions/7554108/javascript-window-location-in-new-tab) – Vaibhav Singh Nov 15 '20 at 12:28

3 Answers3

2

You can try to use window.open() instead of window.location()

window.open(
  $(this).find('a').attr('href'),
  '_blank' // <- This is what makes it open in a new window.
);
Ozgur Sar
  • 2,067
  • 2
  • 11
  • 23
  • This is not working. It just do nothing on click. Maybe because new popups are blocked in modern browsers? – Michal Nov 15 '20 at 12:27
  • @Michal yes it maybe get blocked but there is a workaround explained here https://stackoverflow.com/questions/6628949/window-open-popup-getting-blocked-during-click-event – Ozgur Sar Nov 15 '20 at 16:22
  • Thank you, I will look at it. – Michal Nov 15 '20 at 20:07
0
var url = document.location.protocol + '//' + document.location.host;
url = url + '/print.php;   
var print_window = window.open(url, 'Print', 'height=600,width=900');
freeliver
  • 11
  • 2
0

Try window.open():

 window.open('http://stackoverflow.com/', 'name');

Function description:

name is a name of the window. Following names are supported:

  • _blank - URL is loaded into a new tab. This is default.
  • _parent - URL is loaded into the parent frame
  • _self - URL replaces the current page
  • _top - URL replaces any framesets that may be loaded
Utonium
  • 474
  • 4
  • 10
  • This is not working. It just do nothing on click. Maybe because new popups are blocked in modern browsers? – Michal Nov 15 '20 at 12:28