Link opened in new tab for 'external' links by default

Forums » Opera for Windows/Mac/Linux » Desktop wish-list

You need to be logged in to post in the forums. If you do not have an account, please sign up first.

Go to last post

2. May 2010, 10:48:18

Roccobot

Posts: 33

Link opened in new tab for 'external' links by default

OK; I admit I was a Tab Mix Plus addicted when I used Firefox.. but this feature is very incredibly useful, I think:
I'd like if links were opened in new tabs when they bring to a link of a domain different from the current one. I hope I explained it well bigsmile

2. May 2010, 11:01:18

Frenzie

Posts: 15541

Try something like this.
for (var i=0; var x=document.links[i]; i++) {
  if (x.hostname != location.hostname) {
    x.target = '_blank';
  }
}
The DnD Sanctuary — a safety net for My Opera's demise.

2. May 2010, 15:03:28

Roccobot

Posts: 33

Oh, thank you very much for your answer. Should I implement it as a UserJS? I'm not sure how to do it.. :clown:

2. May 2010, 19:53:32

Frenzie

Posts: 15541

Correct. Note that I haven't tested the code, but it should be something along these lines.
document.addEventListener(
  'DOMContentLoaded',
  function()
  {
    for (var i=0; var x=document.links[i]; i++) {
      if (x.hostname != location.hostname) {
        x.target = '_blank';
      }
    }
  },
  false
);
The DnD Sanctuary — a safety net for My Opera's demise.

2. May 2010, 20:24:30

Roccobot

Posts: 33

Thank you very much, Frenzie. I wouldn't harass you, but I tried with this JS and it didn't work sad
External Links Test.user.js

whistle whistle whistle whistle whistle

2. May 2010, 20:41:55

Frenzie

Posts: 15541

If you're doing that you should use what I originally posted.

i.e. either a file named something like external-links.user.js with
// ==UserScript==
// @name External Links
// ==/UserScript==

for (var i=0; var x=document.links[i]; i++) {
  if (x.hostname != location.hostname) {
    x.target = '_blank';
  }
}


or a file named something like external-links.js with the code I posted above.

Or you could use what you put in that file, but then you'll need to replace DOMContentLoaded with load.

And harassing would be sending me PMs excessively or something, lol.
The DnD Sanctuary — a safety net for My Opera's demise.

2. May 2010, 21:49:32

Roccobot

Posts: 33

'Disturb' was the correct word, maybe (it's an English interface problem) bigsmile
OK, I cannot manage to do it and I've tried a lot of times, thank you very much anyway for the effort, Frenzie pirate

3. May 2010, 06:22:20

Frenzie

Posts: 15541

I messed a little thing up in the syntax earlier because I didn't test it. This should work.

external-links-blank.user.js
The DnD Sanctuary — a safety net for My Opera's demise.

3. May 2010, 14:33:38

Roccobot

Posts: 33

Thank you so much again, Frenzie. But it does not work :-(

3. May 2010, 18:59:50

Frenzie

Posts: 15541

Really? It works for me on, for example, my blog. Without the script links outside my blog (recognizable by the little blue arrow) open in the same window, whereas with the script they automatically open in a new tab.
The DnD Sanctuary — a safety net for My Opera's demise.

4. May 2010, 07:57:00

Roccobot

Posts: 33

Oh, you're right. Thank you!! beer
Maybe there are some site where it doesn't work (like Google Images results), but normally it does.beer beer

4. May 2010, 08:03:07

Frenzie

Posts: 15541

Google links to itself which then redirects you in most cases, so yeah, that wouldn't be detected by this script.
The DnD Sanctuary — a safety net for My Opera's demise.

Forums » Opera for Windows/Mac/Linux » Desktop wish-list