Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you have a template file that is parsed where you can output PHP->Smarty assigned variables, something you could do is create a global JS variable in the template, and then use that global variable within your JS as normal.</p> <p>Such as:</p> <p><strong>Template file</strong></p> <pre><code>&lt;script type="text/javascript"&gt; var MyGlobalVar = "{$MyGlobalVar}"; &lt;/script&gt; </code></pre> <p><strong>Global.js file</strong></p> <pre><code>$(document).ready(function(){ $("button").click(function(){ if (MyGlobalVar != '') { $("#div1").html(MyGlobalVar); } else { alert('Error! Error! Abort!'); } }); }); </code></pre> <p>Note, you could output the Global.js file with it being parsed by Smarty (although... this is probably not a great idea) and inject your PHP->Smarty variables this way. This would treat the Global.js included file as a Smarty template. </p> <p>To do so, you would need to use <a href="http://www.smarty.net/docsv2/en/language.function.literal" rel="nofollow noreferrer"><code>{literal}</code></a>, probably name the file with a .php file ending (so it was PHP-parseable), and add a PHP <code>header()</code> call so PHP outputs the file contents to the browser as a Javascript content-type.</p> <p><strong>Global.js</strong></p> <pre><code>&lt;?php header("content-type: text/javascript"); ?&gt; var MyGlobalVar = "{$MyGlobalVar}"; {literal} $(document).ready(function(){ $("button").click(function(){ if (MyGlobalVar != '') { $("#div1").html(MyGlobalVar); } else { alert('Error! Error! Abort!'); } }); }); {/literal} </code></pre> <p>Additionally, on the PHP side, you might want to consider adding slashes to your variable, especially if the JS variable will contain html or other bits of text that will use single/double quotes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload