Вее работает как ajax ? загрузку скриптов подтянул , но все равно грузит все tpl включает меню верхнее и боковое , вроде все правильно .. но поему грузит все не пойму..
Display All
Source Code
- <script>
- function initMenu() {
- document.querySelectorAll('.nav-link').forEach(item => {
- item.addEventListener('click', () => {
- loadBlock(item.dataset.url, item.dataset.block);
- });
- });
- }
- function loadBlock(url, blockId) {
- fetch(url)
- .then(response => {
- if (!response.ok) throw new Error('error');
- return response.text();
- })
- .then(html => {
- const parser = new DOMParser();
- const doc = parser.parseFromString(html, 'text/html');
- const targetBlock = doc.getElementById(blockId);
- const contentDiv = document.getElementById('content');
- document.querySelectorAll('script[data-dynamic]').forEach(s => s.remove());
- if (targetBlock) {
- contentDiv.innerHTML = '';
- // Клонируем только нужный блок
- const blockClone = targetBlock.cloneNode(true);
- contentDiv.appendChild(blockClone);
- const scripts = blockClone.querySelectorAll('script');
- scripts.forEach(script => {
- const newScript = document.createElement('script');
- newScript.setAttribute('data-dynamic', '');
- if (script.src) {
- newScript.src = script.src;
- newScript.async = script.async;
- newScript.defer = script.defer;
- } else {
- newScript.textContent = script.textContent;
- }
- document.body.appendChild(newScript);
- });
- } else {
- contentDiv.innerHTML = '<div style="color:red;">Блок с ID "' + blockId + '"</div>';
- }
- })
- .catch(error => {
- document.getElementById('content').innerHTML =
- '<div style="color:red;">error: ' + error.message + '</div>';
- });
- }
- document.addEventListener('DOMContentLoaded', initMenu);
- </script>
The post was edited 1 time, last by DenPUBG ().