Skip to content

Instantly share code, notes, and snippets.

@nocodesupplyco
Created December 19, 2022 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nocodesupplyco/bd23afb6202f68343787879bb656c3bb to your computer and use it in GitHub Desktop.
Save nocodesupplyco/bd23afb6202f68343787879bb656c3bb to your computer and use it in GitHub Desktop.
Load or Nest Content From Another Page
// See an text example of this here: https://codepen.io/cmoen89/pen/vYjRVMK
window.addEventListener("load", (event) => {
fetch("https://example.com")
.then(function (response) {
return response.text();
})
.then(function (html) {
// Convert the HTML string into a document object
var parser = new DOMParser();
var doc = parser.parseFromString(html, "text/html");
// Get a specific element in the HTML
var element = doc.querySelector("#load-source");
// Add element to current page
document.getElementById("load-destination").innerHTML = element.innerHTML;
})
.catch(function (err) {
// There was an error
console.warn("Fetched html error", err);
});
});
$(function() {
$('#load-destination').load("https://example.com #load-source");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment