const controlTagPath = '/view/control/banner'; const CONTROL_TAG_DOM = getControlScriptTagDom(controlTagPath); loadControlContent("https://kyujin.navitime.co.jp/view/control/banner?tag=false&", CONTROL_TAG_DOM); function loadControlContent(requestUrl, tagDom) { fetch(requestUrl) .then(res => res.text()) .then((text) => { var div = document.createElement('div'); div.innerHTML = text tagDom.after(div); // スクリプトタグの後ろに挿入 }) .catch((err) => { console.log(err) }); } function getControlScriptTagDom(path, id=null) { const scripts = document.getElementsByTagName('script'); for (let i = 0; i < scripts.length; i++) { const url = scripts[i].src; if (url.indexOf(path) > -1) { if (id == null || id.length === 0) { return scripts[i]; } else { return document.getElementById(id); } } } }