Is it possible to call the js-function from the MaterializeCSS framework from php-code? I try to do by analogy with alert (), but it does not work ..

if($condition) { //echo "<script>alert('foo')</script>"; echo "<script>Materialize.toast('bar')</script>"; } 
  • Do you need it when loading the page? If yes, then <script> Materialize.toast ('bar') </ script> Add to the very end of the page code. - Petr Chalov

2 answers 2

Most likely your solution does not work due to the fact that the call Materialize.toast ('bar') works with DOM, and the DOM is not ready yet. To solve the problem, you need to wait until the DOM tree is ready and then run your code.

native js solution

 <?php if ($condition) { ?> <script type="text/javascript"> function onDomReady() { Materialize.toast('bar') } document.addEventListener("DOMContentLoaded", onDomReady); </script> <?php } ?> 

    Try this

     echo "<script>$(document).ready(function() { Materialize.toast('bar') });</script>"; 
    • The author has not added a jQuery tag to the question. Therefore, most likely he needs a solution on pure js - Kison
    • @kison, as far as I know, jQuery is "sewn" in Materialize - Dev
    • materializecss.com/getting-started.html#setup , in the manual they connect jquery separately, so I believe that your judgment may be wrong. - Kison
    • But the fact that Materialize does not work without jquery is a fact) - Kison