Skip to content

Commit 7e87ab1

Browse files
committed
Merge branch 'sshirokov-pjax-with-scripts'
2 parents 12c98fb + 0572cbd commit 7e87ab1

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

jquery.pjax.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ function pjax(options) {
260260

261261
if (container.title) document.title = container.title
262262
context.html(container.contents)
263+
executeScriptTags(container.scripts)
263264

264265
// Scroll to top by default
265266
if (typeof options.scrollTo === 'number')
@@ -630,6 +631,10 @@ function extractContainer(data, xhr, options) {
630631

631632
// Then scrub any titles from their descendents
632633
obj.contents.find('title').remove()
634+
635+
// Gather all script[src] elements
636+
obj.scripts = findAll(obj.contents, 'script[src]').remove()
637+
obj.contents = obj.contents.not(obj.scripts)
633638
}
634639

635640
// Trim any whitespace off the title
@@ -638,6 +643,33 @@ function extractContainer(data, xhr, options) {
638643
return obj
639644
}
640645

646+
// Load an execute scripts using standard script request.
647+
//
648+
// Avoids jQuery's traditional $.getScript which does a XHR request and
649+
// globalEval.
650+
//
651+
// scripts - jQuery object of script Elements
652+
//
653+
// Returns nothing.
654+
function executeScriptTags(scripts) {
655+
if (!scripts) return
656+
657+
var existingScripts = $('script[src]')
658+
659+
scripts.each(function() {
660+
var src = this.src
661+
var matchedScripts = existingScripts.filter(function() {
662+
return this.src === src
663+
})
664+
if (matchedScripts.length) return
665+
666+
var script = document.createElement('script')
667+
script.type = $(this).attr('type')
668+
script.src = $(this).attr('src')
669+
document.head.appendChild(script)
670+
})
671+
}
672+
641673
// Internal: History DOM caching class.
642674
var cacheMapping = {}
643675
var cacheForwardStack = []

test/evaled.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
window.evaledSrcScript = true
2+
window.evaledScriptLoaded()

test/unit/pjax.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@ if ($.support.pjax) {
118118
})
119119
})
120120

121+
asyncTest("evals scripts", function() {
122+
var frame = this.frame
123+
124+
frame.evaledScriptLoaded = function() {
125+
equal(true, frame.evaledSrcScript)
126+
equal(true, frame.evaledInlineScript)
127+
start()
128+
}
129+
frame.$.pjax({
130+
url: "scripts.html",
131+
container: "#main"
132+
})
133+
})
134+
121135

122136
asyncTest("container option accepts String selector", function() {
123137
var frame = this.frame

test/views/scripts.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<p>Got some script tags here</p>
2+
<script type="text/javascript" src="/test/evaled.js"></script>
3+
<script type="text/javascript">window.evaledInlineScript = true</script>
4+
<script type="text/javascript">window.parent.iframeLoad(window)</script>

0 commit comments

Comments
 (0)