Search code examples
typo3neoscms

TYPO3 Neos: Run inline Javascript after reload


If there are inline JavaScript in the template and after apply changes to the content, what is to do to execute it again?

Just a simple example:

<head>
  <script type="text/javascript">
    console.log('hello world');
  </script>
</head>

The first time loading the page you get 'hello world'. And now change something and click "Apply". Nothing happens.

What can I do?


Solution

  • I think the problem is that once the page is initially loaded(and your script ran), all the other actions do not reload the page because these request are asynchronous. When you navigate to a page, its content gets loaded and inserted. Scripts will not be executed this way.

    Fortunately, TYPO3 Neos fires some JavaScript events(e.g when the page was loaded or a node was created) which you can listen on to invoke your functions. See the offical documentation for a list of all fired events and tips.

    e.g:

    function sayHelloWorld() {
      console.log('hello world');
    }
    document.addEventListener('Neos.PageLoaded', function(event) {
      //This will fire whenever the page reloads by Ajax
      sayHelloWorld();
    }, false);