Skip to content

Instantly share code, notes, and snippets.

@juandefelix
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save juandefelix/4135fce696ea32a298f3 to your computer and use it in GitHub Desktop.

Select an option

Save juandefelix/4135fce696ea32a298f3 to your computer and use it in GitHub Desktop.

Revisions

  1. juandefelix revised this gist May 12, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions Turbolinks_and_Javascript.md
    Original 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)
    [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)
  2. juandefelix revised this gist May 12, 2014. 1 changed file with 13 additions and 4 deletions.
    17 changes: 13 additions & 4 deletions Turbolinks_and_Javascript.md
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,21 @@
    ###Turbolinks and Javascrit###
    ###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).
    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 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
    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 usual waythe DOM is loaded.
    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)
  3. juandefelix created this gist May 12, 2014.
    12 changes: 12 additions & 0 deletions Turbolinks_and_Javascript.md
    Original 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.