Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
possible duplicate of Access PHP var from external javascript file – Ben Lee Oct 4 '11 at 6: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 Varga Oct 4 '11 at 6:51

4 Answers

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.

share|improve this answer

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.

share|improve this answer

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; ?>);
?>
share|improve this answer

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"

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.