Last active
August 29, 2015 14:01
-
-
Save juandefelix/4135fce696ea32a298f3 to your computer and use it in GitHub Desktop.
Revisions
-
juandefelix revised this gist
May 12, 2014 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,6 +16,6 @@ $(document).on("page:change", function() { }); ``` Then your Javascript code will run as expected. There's some links where you can read more about this: [Boris Staal Blog](http://staal.io/blog/2013/01/18/dangers-of-turbolinks/) [Rails Guides](http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#how-turbolinks-works) [Turbolinks](https://github.com/rails/turbolinks/blob/master/README.md#events) -
juandefelix revised this gist
May 12, 2014 . 1 changed file with 13 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,21 @@ ###Turbolinks and Javascrit You might have some weird behavior in your rails app if you use Turbolinks and then you write some Javascript code without considerig the way Turbolinks actually works. If you expect your Javascript code to run inside of `$(document).ready()`, you will notice that it doesn't always happen. Your code will run maybe the first time you load the page in the browser, but not the subsequent times. Maybe won't run also when you access a page by clicking on a link (but then it will work when you refresh your page). Using Turbolnks, when you click on a link, your web browswer won't send a request to the server in order to get the content. Instead, it will replace the content of the <body> with the parsed result of an ajax request to the link. This way, all the code that you wrote under ```jquery $(document).ready(function(){ // some more code }) ``` won't run because Turbolinks overrides the usual way the DOM is loaded. In order to make your code work, you can write somet like this: ```jquery $(document).on("page:change", function() { alert("page has loaded!"); }); ``` Then your Javascript code will run as expected. There's some links where you can read more about this: [Boris Staal Blog](http://staal.io/blog/2013/01/18/dangers-of-turbolinks/) [Rails Guides](http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#how-turbolinks-works) [Turbolinks](https://github.com/rails/turbolinks/blob/master/README.md#events) -
juandefelix created this gist
May 12, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ ###Turbolinks and Javascrit### You might have some weird behavior in your rails app if you use Turbolinks and then you write some Javascript code without considerig the way Turbolinks actually works. If you expect your Javascript code to run when the document is ready you will notice that it doesn't always happen. Your code will run maybe the first time you load the page in the browser, but not the subsequent times. Maybe won't run also when you access a page by clicking on a link (but then it will work when you refresh your page). Using Turbolnks, when you click a link, your web browswer doesn't send a request to the server in order to get the content. Instead, it will replace the content of the <body> with the result of an ajax request to the link. This way, all the code that you wrote under ```jquery $(document).ready(function(){ // some more code }) ``` won't run because Turbolinks overrides usual waythe DOM is loaded.