0

I (absolute php beginner) was given a script with different variables that are based on date and time on the top of xhtml strict page:

<?php

$var1="2011,9,31,18,0,0";

[...]

?>

Inside the html body I have a javascript that currently starts like this:

<script type="text"/javascript">

dateFuture = new Date(<?php echo $var1; ?>);

[...]

</script>

Is it possible to make the javascript external, but still pull the variable $var1 from the top of the index page and then have it show the same output on the index page as it currently does?

I have found one example where the beginning of the external .js is supposed to look like this:

dateFuture = new Date(<?php include("/index.html");echo $var1;?>);

Unfortunately that doesn't seem to work. Is there any possible solution for this? Any help is greatly appreciated. Thank you in advance.

Lux
  • 1,540
  • 1
  • 22
  • 28
user977353
  • 35
  • 1
  • 2
  • 4
  • possible duplicate of [Access PHP var from external javascript file](http://stackoverflow.com/questions/2928827/access-php-var-from-external-javascript-file) – Ben Lee Oct 04 '11 at 06:49
  • That should work perfectly fine, what's the error that you're receiving, and where is index.html? Is it actually in document root? – Christian Oct 04 '11 at 06:51
  • IMO this more recent similar question has very good answer. Read it if you came to this question for answer: [link](http://stackoverflow.com/a/9653766/2628285) – Repeat Spacer Jul 30 '13 at 16:58

5 Answers5

3

Yes. Make the javascript variable global and you can access it inside you external js file. Something like this

<script type="text/javascript">
  var dateFuture = new Date(<?php echo $var1; ?>);
</script>
<script src="your-external-js-file.js" type="text/javascript"></script>

In your-external-js-file.js, you can access the dateFuture.

Or you can encapsulate the code in external js file in a class and pass on the date from php as a parameter to the constructor of that class.

Srisa
  • 967
  • 1
  • 7
  • 17
1

The external JavaScript file itself can point to a PHP file — provided that the PHP file outputs valid JavaScript. That way, you can do something like the following:

myJS.php:

<?php

// Initialize your PHP variable(s) here, or include the PHP script(s) to do so.
$var1="2011,9,31,18,0,0";

...

?>

dateFuture = new Date(<?php echo $var1; ?>);

In your HTML file:

<script type="text/javascript" src="myJS.php"></script>

Since the output of myJS.php is purely JavaScript code, the file extension will not matter to the browser. Same way as your PHP code currently outputs purely HTML code, and your browser understands how to parse that as well.

Jon Newmuis
  • 25,722
  • 2
  • 45
  • 57
1

If your purpose is to move the javascript code to an external script for better modularization, you can move it to a php file and then reference php file as javascript.

<script type="text/javascript" src="myscript.php"></script>

Inside myscript.php -

<?
Header("content-type: text/javascript");
dateFuture = new Date(<?php echo $var1; ?>);
?>
ronakg
  • 4,038
  • 21
  • 46
0

Yes. It is possible. However, you are going to have to make the javascript file into a php file, or force Apache, or whatever web server you use, to run javascript as php (perfectly harmless, because all code outside of <?php ... ?> is just written to output).

In my-external-js.php or my-external-js.js (whichever you choose, though I would recommend the former, because it requires less configuration):

<?php require_once 'file-which-defines-var1.php'; ?>

dateFuture = new Date("<?php print $var1; ?>");

Note: you should always use require_once instead of include or require, so that the same file is never included twice, which leads to messed-up variables and colliding functions/classes. Also, require and require_once case Fatal errors if the script could not be loaded, while include and include_once do not.

Lux
  • 1,540
  • 1
  • 22
  • 28
0

Whilst the answers given by Jonathan Newmuis and RonakG are perfectly acceptable and will work, the purpose of this answer is to answer your question as close to the setup you've got now as possible. However I'd personally agree with RonakG's answer.

If you're using Apache on your server then add the following line to your .htaccess file: AddType application/x-httpd-php .js. Alternatively you could add that code into the Apache configuration if performance is an concern for you.

The goal of that code is, essentially, to say that PHP should parse all files ending in ".js" as if they were ".php"