How to open all the links in the block #blok_1 and #blok_3 in a new tab on js , so as not to add any code to the links themselves?

 #blok_1, #blok_2, #blok_3 { height: 30px; width: 200px; padding: 6px; border-radius: 4px; -moz-border-radius: 4px; -webkit-border-radius: 4px; margin-top: 8px; margin-left: 8px; border: 1px solid #fff; background-color: #FDF5D9; } a { color: #0D6AF3; font: bold 14px Arial, Helvetica; text-decoration: none; } a:hover { color: #F00; } 
 <div id="blok_1"> <a href="http://www.php.su/">открыть в новом окне</a> </div> <div id="blok_2"> <a href="http://www.php.su/">открыть в текущем окне</a> </div> <div id="blok_3"> <a href="http://www.php.su/">открыть в новом окне</a> </div> 

    2 answers 2

    For these purposes, there are events and the window.open() function. The event is accordingly on click . The window.open(url, target, params) function window.open(url, target, params) takes two parameters, the new page url and 'target', this is the name of the new page, usually _blank and parameters for the new tab are used to open a new tab, their values ​​can be viewed in MDN .

    You can do this in two ways.

    The first is to create a function that accepts a DOM object, in which you need to click on links in a new window, you will have to call this function for each block, which should have this behavior:

     function inNewWindow(block) { block.on('click', 'a:not([href^="#"])', function(evt) { evt.preventDefault(); window.open(evt.target.href, '_blank'); }) } 
    1. create an event handler for the wrapper itself (in our case it will be #blok_1 and #blok_3 ). In case the links can appear inside the block dynamically. You can hang the event directly on the links, the case will not change;
    2. Only links will be filtered by their selector: a:not([href^="#"]) or a[href^="http://"] and a[href^="https://"] ; I want the transition to be based on the link, and not on the anchor.
    3. in the handler, stop processing the default event so that the transition inside the page is not perfect (you need to look so that it does not damage the general logic of the site) and make the transition using window.open() ;
    4. accordingly work with this function like this: inNewWindow(document.getElementById('blok_1')) .

    The second is a little better, but you have to add an attribute to the elements.

    1. Let's add to the elements, which should be a transition to a new page, with a data attribute, for example, data-newwindow ;
    2. we will select all elements with such an attribute and hang on them the same handler as in the first variant.
    3. then you just have to add this to the script:

     $('[data-newwindow]').on('click', function(evt) { evt.preventDefault(); window.open(evt.target.href, '_blank'); }); 

      Put listeners on the click event on all the elements of interest to us, then we look at what is written in the href attribute, and open a new window using the window.open method. Jquery:

       $('div a').on('click', function() { var href = $(this).attr('href'); window.open(href, '_blank').focus(); }); 
      • You can add event.preventDefault(); to cancel any other action on the event. - Fenex
      • The link opens in a new tab, but the page on which the links are located also goes to the new address. - user185447
      • @Muson to account for this, I wrote a comment - Fenex
      • How to make links open in a new window, only in blocks with id # blok_1, # blok_3 - user185447
      • @Muson, make the appropriate selection of elements: $('#block1, #block2') - Fenex