Skip to content
This repository has been archived by the owner on Jul 28, 2018. It is now read-only.

Commit

Permalink
Add ability to re-run scripts on back/forward navigation
Browse files Browse the repository at this point in the history
...by specifying data-turbolinks-eval=true on the <script> tag.
  • Loading branch information
Thibaut committed Apr 18, 2015
1 parent aa547cf commit 7a80936
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@

*Nick Reed*

* Add ability to re-run scripts on back/forward navigation by specifying `data-turbolinks-eval=true` on the `<script>` tag.

*Thibaut Courouble*, *Kevin Hughes*

## Turbolinks 2.5.3 (December 8, 2014)

* Prevent the progress bar from filling the entire screen in older versions of Safari.
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ As a rule of thumb when switching to Turbolinks, move all of your javascript tag
</script>
```

Turbolinks will not re-evaluate script tags on back/forward navigation, unless their `data-turbolinks-eval` attribute is set to `true`:

```html
<script type="text/javascript" data-turbolinks-eval=true>
console.log("I'm run on every page load, including history back/forward");
</script>
```

Triggering a Turbolinks visit manually
---------------------------------------

Expand Down
7 changes: 4 additions & 3 deletions lib/assets/javascripts/turbolinks.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ changePage = (doc, options) ->
CSRFToken.update csrfToken if csrfToken?
setAutofocusElement()

executeScriptTags() unless options.runScripts is false
scriptsToRun = if options.runScripts is false then 'script[data-turbolinks-eval="true"]' else 'script:not([data-turbolinks-eval="false"])'
executeScriptTags(scriptsToRun)
currentState = window.history.state

triggerEvent EVENTS.CHANGE
Expand Down Expand Up @@ -184,8 +185,8 @@ onNodeRemoved = (node) ->
jQuery(node).remove()
triggerEvent(EVENTS.AFTER_REMOVE, node)

executeScriptTags = ->
scripts = document.body.querySelectorAll 'script:not([data-turbolinks-eval="false"])'
executeScriptTags = (selector) ->
scripts = document.body.querySelectorAll(selector)
for script in scripts when script.type in ['', 'text/javascript']
copy = document.createElement 'script'
copy.setAttribute attr.name, attr.value for attr in script.attributes
Expand Down
1 change: 1 addition & 0 deletions test/javascript/iframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
<div id="permanent" data-turbolinks-permanent>permanent content</div>
<div id="temporary" data-turbolinks-temporary>temporary content</div>
<script>window.i = window.i || 0; window.i++;</script>
<script data-turbolinks-eval="true">window.k = window.k || 0; window.k++;</script>
</body>
</html>
4 changes: 3 additions & 1 deletion test/javascript/turbolinks_visit_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ suite 'Turbolinks.visit()', ->
if change is 1
@document.addEventListener 'page:fetch', -> fetchCalled = true
assert.equal @window.i, 1
assert.equal @window.k, 1
assert.equal @window.j, 1
assert.equal @document.title, 'title 2'
assert.notOk @document.querySelector('#div')
Expand All @@ -172,7 +173,8 @@ suite 'Turbolinks.visit()', ->
, 0
else if change is 2
assert.notOk fetchCalled
assert.equal @window.i, 1
assert.equal @window.i, 1 # normal scripts are not re-run
assert.equal @window.k, 2 # data-turbolinks-eval="true" scripts are re-run
assert.equal @window.j, 1
assert.equal @document.title, 'title'
assert.notOk @document.querySelector('#new-div')
Expand Down

0 comments on commit 7a80936

Please sign in to comment.