OhReallyJS


script-externallinks.js

Synopsis

This script will add target="_blank" to all links that point to another domain than the current domain.
In other words: if your website is at www.example.com, links to any website that is not www.example.com will open in a new tab or wіndow (depending on your visitor's configuration).

Links that already have a target attribute will be skipped by default, but see the Usage section of this page for instructions on configuring it to overwrite existing target attributes.

License

Copyright © 2016 Rob la Lau <https://ohreally.nl/>

This work is licensed under a Creative Commons Attribution 4.0 International License (https://creativecommons.org/licenses/by/4.0/deed.en).

Please be aware that minimizing this script does not mean that you don't have to include the copyright and author info.

Download

After reading the license information, you can download the current version of script-externallinks.js by clicking → heregoing to https://js.ohreally.nl/download/script-externallinks.js

Please, host the script with your own website instead of linking to the scripts at js.ohreally.nl directly.
Read thisRead https://js.ohreally.nl/hiy if you want to know why.

Dependencies

This script depends on script-url.js (https://js.ohreally.nl/info-url).

To configure this script to overwrite existing target attributes, you need script-querystring.js as well (https://js.ohreally.nl/info-querystring).

See Browser compatibility below for the list of supported/supporting browsers.

Installation

Download and install script-url.js.

Save script-externallinks.js to the root of your website ( http://www.example.com/script-externallinks.js ).

Put this in the <head/> section of your webpages:

<script type="text/javascript" src="/script-url.js"></script>
<script type="text/javascript" src="/script-externallinks.js"></script>

Usage

This script will automatically detect and rewrite links to external websites as soon as the page is loaded. Links that already have a target attribute will be skipped.

If you want this script to overwrite existing target attributes, download and install script-querystring.js, and change the <script/> tag to:

<script type="text/javascript" src="/script-externallinks.js?overwrite=yes"></script>

Examples

If you look at the source code for this pageany page at https://js.ohreally.nl/ (Ctrl-U in Firefox), you'll see that the link to the license does not have a target attribute. But if you look at the Document Object Model (Ctrl-Shift-C in Firefox), you'll see that it does. This is because it was added later by this script.

Browser compatiblity

Firefox yes
Chrome yes
Internet Explorer yes
Opera yes
Safari yes

Found a browser that works, but is not listed? Please, let me know (https://js.ohreally.nl/contact).

Bug reports

Don't get your hopes up too high for feature requests (to me the script seems rather complete), but if you want to report a bug, feel free to use the contact form (https://js.ohreally.nl/contact).