Skip to content

Commit 6a84281

Browse files
authored
Merge pull request #5822 from Laravel-Backpack/fix-scripts-on-table
fix scripts on table ajax request
2 parents 19d567a + 8e81ff0 commit 6a84281

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

src/resources/views/crud/components/datatable/datatable_logic.blade.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,51 @@ function setupTableEvents(tableId, config) {
559559
// button created by the developer. For that reason, we move them to the end of the body
560560
// ensuring they are re-evaluated on each draw event.
561561
document.getElementById(tableId).querySelectorAll('script').forEach(function(script) {
562-
const newScript = document.createElement('script');
563-
newScript.text = script.text;
564-
document.body.appendChild(newScript);
562+
const scriptsToLoad = [];
563+
if (script.src) {
564+
// For external scripts with src attribute
565+
const srcUrl = script.src;
566+
567+
// Only load the script if it's not already loaded
568+
if (!document.querySelector(`script[src="${srcUrl}"]`)) {
569+
scriptsToLoad.push(new Promise((resolve, reject) => {
570+
const newScript = document.createElement('script');
571+
572+
// Copy all attributes from the original script
573+
Array.from(script.attributes).forEach(attr => {
574+
newScript.setAttribute(attr.name, attr.value);
575+
});
576+
577+
// Set up load and error handlers
578+
newScript.onload = resolve;
579+
newScript.onerror = reject;
580+
581+
// Append to document to start loading
582+
document.head.appendChild(newScript);
583+
}));
584+
}
585+
586+
// Remove the original script tag
587+
script.parentNode.removeChild(script);
588+
} else {
589+
// For inline scripts
590+
const newScript = document.createElement('script');
591+
592+
// Copy all attributes from the original script
593+
Array.from(script.attributes).forEach(attr => {
594+
newScript.setAttribute(attr.name, attr.value);
595+
});
596+
597+
// Copy the content
598+
newScript.textContent = script.textContent;
599+
600+
try {
601+
document.head.appendChild(newScript);
602+
}catch (e) {
603+
console.warn('Error appending inline script:', e);
604+
}
605+
}
606+
565607
});
566608
567609
// Run table-specific functions and pass the tableId

0 commit comments

Comments
 (0)