summaryrefslogtreecommitdiff
path: root/resources
diff options
context:
space:
mode:
authorGreg Roach <greg@subaqua.co.uk>2026-02-11 00:31:28 +0000
committerGreg Roach <greg@subaqua.co.uk>2026-02-13 15:07:42 +0000
commita3ca8e1fef2494b490207b6ce4549d602bcc24a8 (patch)
treec1437477fbf87837cc4bf51d61e2e864a8cf17ad /resources
parentab5cd4f311b8e85fb173c2147fe45ade76f0266a (diff)
downloadwebtrees-a3ca8e1fef2494b490207b6ce4549d602bcc24a8.tar.gz
webtrees-a3ca8e1fef2494b490207b6ce4549d602bcc24a8.tar.bz2
webtrees-a3ca8e1fef2494b490207b6ce4549d602bcc24a8.zip
Convert jQuery code to VanillaJS
Diffstat (limited to 'resources')
-rw-r--r--resources/js/webtrees.js56
1 files changed, 53 insertions, 3 deletions
diff --git a/resources/js/webtrees.js b/resources/js/webtrees.js
index e566b5e57d..472832bbb2 100644
--- a/resources/js/webtrees.js
+++ b/resources/js/webtrees.js
@@ -56,6 +56,56 @@
}
/**
+ * Simple replacement for jQuery().load() - fetch HTML, insert into an element, and execute and scripts.
+ *
+ * @param {element} element
+ * @param {string} url
+ * @param {object|null} data
+ */
+ webtrees.load = function (element, url, data = null) {
+ const csrfToken = document.head.querySelector('meta[name=csrf]').getAttribute('content');
+
+ const options = {
+ body: data,
+ method: data === null ? 'GET' : 'POST',
+ headers: new Headers({
+ 'accept': 'text/html',
+ 'x-requested-with': 'XMLHttpRequest',
+ 'x-csrf-token': csrfToken,
+ }),
+ };
+
+ fetch(url, options)
+ .then(response => {
+ return response.text();
+ })
+ .then(html => {
+ element.innerHTML = html;
+
+ // Parse into a document fragment
+ const parser = new DOMParser();
+ const doc = parser.parseFromString(html, "text/html");
+
+ // Append script elements to the end of the body.
+ Array.from(doc.body.childNodes).forEach(node => {
+ if (node.tagName === "SCRIPT") {
+ let script = document.createElement('script');// Copy attributes (src, type, etc.)
+ Array.from(node.attributes).forEach(attr => {
+ script.setAttribute(attr.name, attr.value);
+ });
+ if (node.src) {
+ script.src = node.src;
+ } else {
+ script.textContent = node.textContent;
+ }
+ node.remove();
+ document.body.appendChild(script);
+ }
+ });
+ });
+ }
+
+ /**
* Simple wrapper around fetch() with our preferred headers
*
* @param {string} url
@@ -914,11 +964,11 @@ $.ajaxSetup({
/**
* Initialisation
*/
-$(function () {
+document.addEventListener('DOMContentLoaded', function() {
// Page elements that load automatically via AJAX.
// This prevents bad robots from crawling resource-intensive pages.
- $('[data-wt-ajax-url]').each(function () {
- $(this).load(this.dataset.wtAjaxUrl);
+ document.querySelectorAll('[data-wt-ajax-url]').forEach(function (element) {
+ webtrees.load(element, element.dataset.wtAjaxUrl);
});
// Autocomplete