0

Maybe is too basic but i could not do it, i have a php file and inside of it i call a js file. Inside that js i need to obtain a get var from the php file.

I have the following

PHP File:

<script language="javascript" src="scripts/scripts.js?tiempo=<?php echo $marcaTiempo; ?>"></script>

and in the scripts.js i need to get the var tiempo.

How can i achieve it?

Thanks

Douglas Roos
  • 613
  • 10
  • 28
  • It is not that, i need to read the var inside the scripts.js, not the php, i don't know how to get it – Douglas Roos Aug 13 '14 at 20:53
  • possible duplicate of [How to get the value from URL Parameter?](http://stackoverflow.com/questions/979975/how-to-get-the-value-from-url-parameter) – leo Aug 13 '14 at 20:53
  • Are you trying to get something from the URL `"scripts/scripts.js?tiempo...`" or are you trying to get something from the resulting script file? Your question is not clear on that. – jfriend00 Aug 13 '14 at 20:59

2 Answers2

3

Option 1

Make the server handle the js file as a php file, and do something like:

<?php
$tiempo = filter_input(INPUT_GET,"tiempo",FILTER_SANITIZE_STRING);
?>
var tiempo = <?php echo $tiempo; ?>;

Obviously not great for caching.

Option 2

Declare the parameter globally:

<script>
 var tiempo=<?php echo $marcaTiempo; ?>
</script>
<script src="scripts/scripts.js"></script>

and in scripts.js:

var tiempo = window.tiempo;
leo
  • 8,106
  • 7
  • 48
  • 80
1

I've solved this in the past by doing the following:

<script type="text/javascript">
  var tiempo = <?php echo $marcaTiempo; ?>;
</script>
<script type="text/javascript" src="scripts/scripts.js"></script>

Then the variable is accessible from your script.

Brian
  • 3,850
  • 3
  • 21
  • 37