I have a big validation JS script. I want to put this script to be external JS.
Everything works fine with this, but in the JS i use cookie to handle the my user form junctions.
And besides, i use PHP in this JS.
If the JS in the html, then works. But if i put it to external, then no.
For example:
External JS:
$(document).ready(function() {
var test = $.cookie('tet');
$.cookie('tet','8');
<?php
if (isset($_SESSION['main_check_a'])) {
echo "$.cookie('tet','1');";
?>
}}
}
How can i pass this PHP code to my external JS?
You need to either
Have the JS file parsed by PHP, by naming it
filename.js.phpor by changing the server’s configuration for PHP to parse .js files. This is not optimal however, because a resource-intensive PHP instance will be started for every script request.Alternatively, much better IMO, do the necessary checks in a
<script>tag in the embedding document, and store the results in JS variables that the embedded external scripts can query. This way, your script files can stay static.In your case, this would mean something like this. In the main document:
this will give you the JavaScript variable
main_check_athat you can use in your external scripts.