Skip to content

Commit

Permalink
Merge pull request #124 from pryn-kb/main
Browse files Browse the repository at this point in the history
js update
  • Loading branch information
pryn-kb authored Aug 29, 2024
2 parents 7fd49a7 + 310a828 commit 8be4dc6
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions _static/custom.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/**
* This script automatically makes external links on a webpage open in a new tab.
*
* It adds an event listener for the 'DOMContentLoaded' event, which ensures that
* the script runs only after the entire HTML document has been fully loaded and parsed.
*
* The script works as follows:
* 1. Select all anchor (`<a>`) tags on the page that have an `href` attribute.
* 2. For each link, the script creates a `URL` object based on the `href` attribute
* and compares the link's origin (protocol + domain) with the current page's origin.
* 3. If the origin of the link differs from the current page's origin, it indicates
* that the link is external.
* 4. The script then sets the `target` attribute of the link to `_blank`, which
* instructs the browser to open the link in a new tab.
*
* Compatibility: The script is compatible with modern browsers that support the
* `URL` constructor and `forEach` method.
*/

document.addEventListener("DOMContentLoaded", function() {
// Select all anchor tags with href attributes
const links = document.querySelectorAll('a[href]');
Expand Down

0 comments on commit 8be4dc6

Please sign in to comment.