/load-with-fetch.js Secret
Created
December 19, 2022 16:44
Star
You must be signed in to star a gist
Load or Nest Content From Another Page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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