using CMSB to manage example.js?

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

By Toledoh - January 24, 2016

Hi Guys.

I want CMSB to maintain elements within a .js file that I would normally link in by something like;

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

Do you see any issues in me having a example.php file with the standard CMSB code, with an inline script:

<script>
// use variable such as <?php echo ($variablesRecord['variable1']) ?>
</script>

Or can I create example.js.php like I do for XML files like sitemap.xml.php?

What would be the best way to go?

Cheers,

Tim (toledoh.com.au)

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