Skip to content

Instantly share code, notes, and snippets.

@jcuffe
Created August 5, 2019 19:21
Show Gist options
  • Save jcuffe/7addcf46752f335260a3031826135ce2 to your computer and use it in GitHub Desktop.
Save jcuffe/7addcf46752f335260a3031826135ce2 to your computer and use it in GitHub Desktop.
class APrefetch extends HTMLAnchorElement {
constructor() {
super()
}
connectedCallback() {
this.addEventListener("click", event => {
event.preventDefault()
const xhr = new XMLHttpRequest()
// Replace elements within the router
xhr.addEventListener("load", function() {
const incoming = this.responseXML.querySelector("body")
const router = document.querySelector("body")
router.innerHTML = incoming.innerHTML
})
xhr.open("GET", this.href)
// Parse the document as XML
xhr.responseType = "document"
xhr.send()
})
}
}
window.customElements.define("a-prefetch", APrefetch, { extends: "a" })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment