Create HTML Document with Custom URL for the document in JavaScript

You can use the concept of createHTMLDocument(). Following is the code −


<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<link rel="stylesheet" href="//">
<script src=""></script>
<script src=""></script>
   const htmlDocument = document.implementation.createHTMLDocument();
   const customURL = htmlDocument.createElement( 'base' );
   customURL.href = "";
   htmlDocument.head.append( customURL );
   console.log("Base URL="+customURL.href);
   const modifiedURL = htmlDocument.createElement("a");
   modifiedURL.href = "../java/java_questions_answers.html";
   htmlDocument.body.append( modifiedURL );
   console.log("After Modifying URL="+ modifiedURL.href );

To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.


This will produce the following output: on console −

Base URL=
After Modifying URL=