Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created October 22, 2022 10:55
Base Tag HREF Doesn't Affect Document Root-Relative URLs
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
</head>
<body>
<h1>
Base Tag HREF Doesn't Affect Document Root-Relative URLs
</h1>
<!--
Our "loader.js" script is stored within the "/assets/" folder. Let's see if we can
load this file using different forms of SRC URLs in conjunction with a BASE tag
HREF that places the current page context inside the assets folder. We're going to
try using both a RELATIVE and a DOCUMENT ROOT-RELATIVE path.
-->
<base href="./assets/" />
<script src="./loader.js?v=relative"></script>
<script src="/loader.js?v=root-relative"></script>
</body>
</html>
// Since the document is going to block-and-wait for this Script tag to load, it means
// that the last <script> element on the page is currently THIS ONE. As such, let's grab
// all of the script tags and then take the one in the last index.
var nodes = document.querySelectorAll( "script" );
var thisNode = nodes[ nodes.length - 1 ];
// Log the "?v=" portion of the SRC attribute.
console.log( "Loaded:", thisNode.src.split( "?" )[ 1 ] );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment