using CMSB to manage example.js?

3 posts by 2 authors in: Forums > CMS Builder
Last Post: January 25, 2016   (RSS)

Hi Tim, 

I've always included my variables that get used in scripts on the page itself:

<!-- set the variable here -->
<script> var foo = <?php echo intval($variablesRecord['variable1']) ?>;</script>

<!-- variable is called in this script -->
<script src="<?php echo PREFIX_URL ?>/assets/js/example.js"></script>

But if you wanted to edit the javascript file directly, you could do so by renaming example.js to example php:

<script src="<?php echo PREFIX_URL ?>/assets/js/example.php"></script>

Then you can ensure that the browser knows the example.php file contains JS by adding the following header, then you can include whatever JS you want after that point:

<?php header("Content-Type: application/javascript"); ?>

var foo = <?php echo intval($variablesRecord['variable1']) ?>;

alert(foo);

Let me know if you have any questions.

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Toledoh - January 25, 2016

Thanks Greg.  I'll use the example.php as I want to re-use those variables through the site. 

Cheers,

Tim (toledoh.com.au)