Skip to content

Commit

Permalink
Merge pull request #30 from SilverFire/scripts
Browse files Browse the repository at this point in the history
Fixed loading of scripts in pjax containers
  • Loading branch information
SilverFire committed Nov 23, 2015
2 parents 2273861 + 9a6383b commit 17c98de
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 37 deletions.
51 changes: 26 additions & 25 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
yii-pjax Change Log
===================

2.0.6 Under development
-----------------
- Enh #25: Blur the focused element if it's inside Pjax container (GeorgeGardiner)

2.0.3 Mar 7, 2015
-----------------
- Chg: Merged changes from upstream (samdark)

2.0.2 Dec 4, 2014
-----------------
- Chg #12: Merged changes from upstream (samdark)

2.0.1 Oct 10, 2014
------------------
- Bug #9: Fixed missing history option in default settings (tonydspaniard)
- New #11: add new option "cache" (macklay)


2.0.0 Mar 20, 2014
------------------
- Bug: Fixed avoid duplicates of _pjax parameter (tof06)
- Bug: Fixed Pjax/GridView and back button (klevron, tof06, tonydspaniard)
yii-pjax Change Log
===================

2.0.6 Under development
-----------------
- Bug #23: Fixed loading of scripts in pjax containers (nkovacs, silverfire)
- Enh #25: Blur the focused element if it's inside Pjax container (GeorgeGardiner)

2.0.3 Mar 7, 2015
-----------------
- Chg: Merged changes from upstream (samdark)

2.0.2 Dec 4, 2014
-----------------
- Chg #12: Merged changes from upstream (samdark)

2.0.1 Oct 10, 2014
------------------
- Bug #9: Fixed missing history option in default settings (tonydspaniard)
- New #11: add new option "cache" (macklay)


2.0.0 Mar 20, 2014
------------------
- Bug: Fixed avoid duplicates of _pjax parameter (tof06)
- Bug: Fixed Pjax/GridView and back button (klevron, tof06, tonydspaniard)
51 changes: 39 additions & 12 deletions jquery.pjax.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ function pjax(options) {
autofocusEl.focus();
}

executeScriptTags(container.scripts)
executeScriptTags(container.scripts, context)

var scrollTo = options.scrollTo

Expand Down Expand Up @@ -774,7 +774,7 @@ function extractContainer(data, xhr, options) {
obj.contents.find('title').remove()

// Gather all script[src] elements
obj.scripts = findAll(obj.contents, 'script[src]').remove()
obj.scripts = findAll(obj.contents, 'script').remove()
obj.contents = obj.contents.not(obj.scripts)
}

Expand All @@ -790,26 +790,53 @@ function extractContainer(data, xhr, options) {
// globalEval.
//
// scripts - jQuery object of script Elements
// context - jQuery object whose context is `document` and has a selector
//
// Returns nothing.
function executeScriptTags(scripts) {
function executeScriptTags(scripts, context) {
if (!scripts) return

var existingScripts = $('script[src]')

scripts.each(function() {
var cb = function (next) {
var src = this.src
var matchedScripts = existingScripts.filter(function() {
var matchedScripts = existingScripts.filter(function () {
return this.src === src
})
if (matchedScripts.length) return

var script = document.createElement('script')
var type = $(this).attr('type')
if (type) script.type = type
script.src = $(this).attr('src')
document.head.appendChild(script)
})
if (matchedScripts.length) {
next()
return
}

if (this.src) {
var script = document.createElement('script')
var type = $(this).attr('type')
if (type) script.type = type
var done = function () {
script.onload = null;
script.onerror = null;
next()
}
script.onload = script.onerror = done
script.src = $(this).attr('src')
document.head.appendChild(script)
} else {
context.append(this)
next()
}
}

var i = 0;
var next = function () {
if (i >= scripts.length) {
return
}
var script = scripts[i]
i++
cb.call(script, next)
}
next()
}

// Internal: History DOM caching class.
Expand Down

0 comments on commit 17c98de

Please sign in to comment.