Simple Machines Community Forum

General Community => Scripting Help => Topic started by: spiros on March 09, 2019, 03:00:29 PM

Title: Opening a link in one tab, following links in that tab
Post by: spiros on March 09, 2019, 03:00:29 PM
EDIT: got it, it was the noopener noreferrer bit that stopped it.

This is in search.template.php [Tab 1]

I want the first link to open in new tab [Tab 2] and any consecutive links that are clicked on [Tab 1] to open in [Tab 2].


<li>
<a class="button_strip_print" href="https://www.google.com/search?q=', !empty($context['search_params']['search']) ? $context['search_params']['search'] : '', '" target="_blank" title="Google search" rel="noopener noreferrer nofollow"><span class="last">G</span></a>
</li>


I tried linking to an external js and renaming _blanks to mytarget, but did not do it.

var targetlinks = document.querySelectorAll('[target="mytarget"]');

var opentarget = function(e){
    if( "mytarget" in window ){
         mytarget.location.href = e.target.href;   
    }
    else {
         window.mytarget = window.open( e.target.href, 'mytarget' );   
    }
    e.preventDefault();
};

[].slice.call( targetlinks ).forEach( function(link){
    link.addEventListener('click', opentarget );
});
Title: Re: Opening a link in one tab, following links in that tab
Post by: Arantor on March 09, 2019, 03:29:02 PM
All you had to do was name the target the same each time; _blank opens in a new tab regardless, but a named target always opens in that target.

noopener noreferrer stops that, of course, and is also potentially a security measure you're getting rid of.
Title: Re: Opening a link in one tab, following links in that tab
Post by: spiros on March 10, 2019, 04:20:06 AM
Yes, you are right, but I will have to make do with that, unless someone knows a different way of doing it that would preserve the security measures...