GithubHelp home page GithubHelp logo

Comments (11)

reed avatar reed commented on July 18, 2024

Would this work?

Remove the script tag add this to your JS:

$ ->
  $(document).on 'page:load', ->
    window.MathJax = null
    $.getScript "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"

I did a small test and it worked for me. Let me know.

from turbolinks-compatibility.

 avatar commented on July 18, 2024

Hi Reed,
As you instructed, I removed the script tag in my layouts/application.html.erb and added the code above to app/assets/javascripts/mathjax.js.coffee. MathJax did not load even in homepage and after refreshing the page multiple times. Then I added the code to app/assets/application.js, but the result was the same and MathJax didn't load.
Thank you very much.

from turbolinks-compatibility.

reed avatar reed commented on July 18, 2024

Sorry, that's my mistake. Try this:

$ ->
  loadMathJax()
  $(document).on 'page:load', loadMathJax

loadMathJax = ->
  if page is Math/New or Physics/Show # optional check
    window.MathJax = null
    $.getScript "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"

If you have a way of checking what page you're on, that optional check is ideal since it will reduce the number of times you fetch and load the MathJax library.

from turbolinks-compatibility.

 avatar commented on July 18, 2024

Hi Reed,
Yessssssss, you made my day. Thanks.
Because I'm using jquery.turbolinks, I had to remove the 3rd line $(document).on 'page:load', loadMathJax in order to avoid loading MathJax twice:

Now, would you please help me with any of the following 3 questions:

  1. Is there a downside to using jquery.turbolinks vs the line above?
  2. Before I was using the following script to deactivate the Math Menu in MathJax
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    showMathMenu: false
  });
</script>

How should I implement this in my .js.coffeescript file now?
3. How did you go about solving question 2?

Thanks again for your help.
Best.

from turbolinks-compatibility.

reed avatar reed commented on July 18, 2024
  1. The only downside to using jquery.turbolinks (in general) is that it takes away the ability to run a callback only on page:load, as opposed to both page:load AND DOM ready. If you would like the functionality of jquery.turbolinks without this restriction, you can use the master branch (v2.0.0) of Turbolinks and then bind to page:change instead of page:load, as it will fire on DOM ready.
  2. Do you need to move it? The MathJax script looks for the script tags with text/x-mathjax-config and runs them, so I would think they would work as they are. Is that not the case? If not, try just adding that config line after the $.getScript line. If that doesn't work, try adding it before the $.getScript line.
  3. Well, I'm just making educated guesses at this point. If neither one works, I'll take a deeper look at the MathJax script and see if I can figure it out.

from turbolinks-compatibility.

 avatar commented on July 18, 2024

Thanks reed, I really appreciate it. If I don't move the text/x-mathjax-config script and leave it in the head section of layouts/application.html.erb, It works only on the homepage and not other pages unless I refresh the page. When I added the config line after or before the $.getScript it didn't work at all.

from turbolinks-compatibility.

reed avatar reed commented on July 18, 2024

Ah, I thought your config script tag was in the body. Regardless, this works for me:

$ ->
  loadMathJax()
  $(document).on 'page:load', loadMathJax

loadMathJax = ->
  if page is Math/New or Physics/Show # optional check
    window.MathJax = null
    $.getScript "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML", ->
      MathJax.Hub.Config
        showMathMenu: false

Or, if you feel like going overboard with it, like I apparently did, you can use this:

class MathJax
  constructor: (@options) ->
    @bind()

  load: =>
    window.MathJax = null
    @_fetch().done =>
      window.MathJax.Hub.Config @options

  bind: ->
    $ => @load()
    $(document).on 'page:load', @load

  unbind: ->
    $(document).off 'page:load', @load

  # private

  _fetch: ->
    $.ajax
      dataType: 'script'
      cache: true
      url: "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=AM_HTMLorMML"

# usage
new MathJax
  showMathMenu: false

This solution takes advantage of jQuery's ajax caching, so you'll only be downloading the script once.

from turbolinks-compatibility.

 avatar commented on July 18, 2024

Wow, Awesome. You really made my day. Thanks a lot.
Best,
Rostam

from turbolinks-compatibility.

reed avatar reed commented on July 18, 2024

Glad I could help. I posted the solution to the site:

http://reed.github.io/turbolinks-compatibility/mathjax.html

from turbolinks-compatibility.

jprinaldi avatar jprinaldi commented on July 18, 2024

Just a quick update:

In Turbolinks 5, you have to use turbolinks:load instead of page:load.

from turbolinks-compatibility.

zengfengbo avatar zengfengbo commented on July 18, 2024

for caching behavior, cache must be disabled, by adding
<meta name="turbolinks-cache-control" content="no-cache">

from turbolinks-compatibility.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.