Skip to content

Commit ed94d44

Browse files
committed
fix: XMLHttpRequest issue
1 parent cd10754 commit ed94d44

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

js/src/ajax-load.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,23 @@ const AjaxLoad = (($) => {
2121
const JQUERY_NO_CONFLICT = $.fn[NAME]
2222

2323
const ClassName = {
24-
ACTIVE : 'active',
25-
NAV_PILLS : 'nav-pills',
26-
NAV_TABS : 'nav-tabs',
27-
OPEN : 'open'
24+
ACTIVE : 'active',
25+
NAV_PILLS : 'nav-pills',
26+
NAV_TABS : 'nav-tabs',
27+
OPEN : 'open',
28+
VIEW_SCRIPT : 'view-script'
2829
}
2930

3031
const Event = {
3132
CLICK : 'click'
3233
}
3334

3435
const Selector = {
36+
HEAD : 'head',
3537
NAV_DROPDOWN : '.sidebar-nav .nav-dropdown',
3638
NAV_LINK : '.sidebar-nav .nav-link',
37-
NAV_ITEM : '.sidebar-nav .nav-item'
39+
NAV_ITEM : '.sidebar-nav .nav-item',
40+
VIEW_SCRIPT : '.view-script'
3841
}
3942

4043
const Default = {
@@ -74,21 +77,48 @@ const AjaxLoad = (($) => {
7477
const element = this._element
7578
const config = this._config
7679

80+
const loadScripts = (src, element = 0) => {
81+
const script = document.createElement('script')
82+
script.type = 'text/javascript'
83+
script.src = src[element]
84+
script.className = ClassName.VIEW_SCRIPT
85+
// eslint-disable-next-line no-multi-assign
86+
script.onload = script.onreadystatechange = function () {
87+
if (!this.readyState || this.readyState === 'complete') {
88+
if (src.length > element + 1) {
89+
loadScripts(src, element + 1)
90+
}
91+
}
92+
}
93+
const body = document.getElementsByTagName('body')[0]
94+
body.appendChild(script)
95+
}
96+
7797
$.ajax({
7898
type : 'GET',
7999
url : config.subpagesDirectory + url,
80100
dataType : 'html',
81-
cache : false,
82-
async: false,
83-
success: function success() {
101+
beforeSend() {
102+
$(Selector.VIEW_SCRIPT).remove()
103+
},
104+
success(result) {
105+
const wrapper = document.createElement('div')
106+
wrapper.innerHTML = result
107+
108+
const scripts = Array.from(wrapper.querySelectorAll('script')).map((script) => script.attributes.getNamedItem('src').nodeValue)
109+
110+
wrapper.querySelectorAll('script').forEach((script) => script.parentNode.removeChild(script))
111+
84112
$('body').animate({
85113
scrollTop: 0
86114
}, 0)
87-
$(element).load(config.subpagesDirectory + url, null, () => {
88-
window.location.hash = url
89-
})
115+
$(element).html(wrapper)
116+
if (scripts.length) {
117+
loadScripts(scripts)
118+
}
119+
window.location.hash = url
90120
},
91-
error: function error() {
121+
error() {
92122
window.location.href = config.errorPage
93123
}
94124
})

0 commit comments

Comments
 (0)