3

Is there a way to get the DOM elements from another HTML file rather than the elements from the HTML file that calls the JS file?

My idea is to make html templates, and render them in a main html file, depending on certain conditions imposed by the JS file.

Something like

var initScreen = new document(URL);
document.getElementById("body") = initScreen.getElementById("body");

supposing that this is the correct way.

1 Answer 1

7

Yep. You can fetch a remote (or local) file and then use createHTMLDocument:

document.querySelector(".the_replacer").addEventListener("click", () => {
    fetch("https://cdn.rawgit.com/ryanpcmcquen/c22afdb4e6987463efebcd495a105d6d/raw/476e97277632e89a03001cb048c6cacdb727028e/other_file.html")
        .then((response) => response.text())
        .then((text) => {
            const otherDoc = document.implementation.createHTMLDocument("Foo").documentElement;
            otherDoc.innerHTML = text;
            document.querySelector(".element_on_main_page").textContent = otherDoc.querySelector(".awesome_external_element").textContent;
        });
});
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="index.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <div class="element_on_main_page">Replace me, please.</div>
    <button class="the_replacer">Replace them.</button>
    <script src="index.js"></script>
</body>

</html>
https://repl.it/@ryanpcmcquen/TurboTremendousProgramminglanguages

Here's the remote file's contents:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="index.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <div class="awesome_external_element">Foobar.</div>
</body>

</html>

Browser support is not too bad. On the other hand fetch is pretty modern, so if you are going for legacy support you should use XMLHttpRequest.

Sign up to request clarification or add additional context in comments.

8 Comments

<link> tags do not use or need a closing slash.
@Rob, I bet they would fix it if you contacted them. Although closing slashes are coming back with a vengeance thanks to React.
The React people need to learn how to read the HTML spec, too.
What if I load an iframe, with the url, and in the first render take them out of the html, and via JS, extract what i need from them, and keep them as variables in the JS code?
@ekiim, there is no need to involve an iframe. As you can see in my answer, everything can be stored in JS variables without the use of an iframe. An iframe just adds needless complexity.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.