Avoid to open (some) links in new tab [solved]

I have a script to open in new tab links beginning with http (/https). This is the (working) code:

$(function(){
    $('[href^="http"]').on('click',function(e){        
        e.preventDefault();
        window.open($(this).attr('href'));
    });
});

But the above code works … too much. I mean it works also in breadcrumbs links, where it would be better not to open a link in a new tab.
This is the breadcrumbs (working) js/php code:

<?php

  // Credit goes to Dominic Barnes - http://stackoverflow.com/users/188702/dominic-barnes
  // http://stackoverflow.com/questions/2594211/php-simple-dynamic-breadcrumb
  // This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the user's current path
  
  function breadcrumbs($separator = ' &raquo; ', $home = 'Home')
  {
    // This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an array, and then filters out any empty values
    $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));

    // This will build our "base URL" ... Also accounts for HTTPS :)
    $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';

    // Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
    $breadcrumbs = array("    <li><a href=\"$base\">$home</a></li>\n");

    // Initialize crumbs to track path for proper link
    $crumbs = '';

    // Find out the index for the last value in our path array
    $last = end(array_keys($path));

    // Build the rest of the breadcrumbs
    foreach ($path as $x => $crumb) {
        // Our "title" is the text that will be displayed (strip out .php and turn '_' into a space)
        $title = ucwords(str_replace(array('.php', '_', '%20'), array('', ' ', ' '), $crumb));
        
        // If we are not on the last index, then display an <a> tag
        if ($x != $last) {
            $breadcrumbs[] = "   <li itemprop='itemListElement' itemscope itemtype='https://schema.org/ListItem'>
            <a href=\"$base$crumbs$crumb\" itemprop='item'>
            <span itemprop='name'>$title</span>
            <meta itemprop='position' content='$x'  />
            </a> »</li>\n";
            $crumbs .= $crumb . '/';
        }
        // Otherwise, just display the title (minus)
        else {
            $breadcrumbs[] = $title;
        }

    }

    // Build our temporary array (pieces of bread) into one big string :)
    return implode($separator, $breadcrumbs);
}

?>

You can see breadcrumbs on my website (www.culturanuova.net, in any subfolder, such as https://www.culturanuova.net/filosofia/1.antica/Aristotele.php)

How could I open (only) the breadcrumbs in the same tab?

Thank you!!

$('#breadcrumb a')

1 Like

Thank you: but where should I put that code?
However meanwhile, I was trying this code, and it seems work:

$(function(){
    $('[href^="http"]'.not("http://localhost" )).on('click',function(e){        
        e.preventDefault();
        window.open($(this).attr('href'));
    });
});

EDIT

No, I was wrong: with my new code all links all opened in the same tab :sweat_smile:

$('[href^="http"]')
says “Find all elements with an href that starts with http”.

$('#breadcrumb a')
says “Find all a elements underneath the element with ID ‘breadcrumb’”

$('[href^="http"]'.not("http://localhost" ))
is nonsensical. What you might be trying to do there is
$('[href^="http"]').not('[href^="http://localhost"]')
which says “Find all elements with an href that starts with http. Now throw away anything that starts with http://localhost, and operate on the remainder.”

1 Like

Thank you!
Indeed your new code works:

$('[href^="http"]').not('[href^="http://localhost"]')

Perfect! :blush: :100:

EDIT

Uhm… sorry but, another small thing: how to add even https://localhost ?

chain another .not on the end.

1 Like

But this code doesn’t work:

$('[href^="http"]').not('[href^="http://localhost"]'||.not('[href^="https://localhost"]')

Why did you turn a ) into a ||?

1 Like

I’m not able to find the right code…
neither this work:

$('[href^="http"]').not('[href^="http://localhost"]')||.not('[href^="https://localhost"]')

Why is there a || in there at all? Stop putting random things into your code :stuck_out_tongue:

2 Likes

ok, thank you!

this is the right one:

$(function(){
	$('[href^="http"]').not('[href^="http://localhost"]').not('[href^="https://localhost"]').on('click',function(e){
        e.preventDefault();
        window.open($(this).attr('href'));
    });
});

Thank you very much for your patience! :heart_eyes:

The above code works, as expected, in local.
I wonder if I could use it also in remote, with not too much code.
Afterward I will try…

Is there a reason you are trying to do this with script rather than just using the target attribute on the links you want to open in a new tab?

<a href="https://example.com" target="_blank">Link</a>
1 Like

Target isn’t a deprecated (by w3c) attribute?

No, not anymore. It was removed/deprecated in HTML 4/XHTML1 strict mode, but those are now abandoned and HTML5 is the current spec where it continues to be valid.

1 Like