views:

149

answers:

3

I want to make some thing like this

my-php-file.php

  $lang = 'es';

my-js-file.js

if ($lang == es)
   {
   something-magical-happens;
   }

or like this:

if (URL == www.mydomain.com/index.php?lang=es)
   {
   something-magical-happens;
   }
+4  A: 

you could generate js on-the-fly

my.js.php:

<?php echo "//Yes." ?>
var i = "<?php echo $_GET['lang']; ?>";

function doSomethingWithI(){
  alert(i);
}

Now, try to include

<script type="text/javascript" src="my.js.php?lang=es"></script>

in your html and you'll see :)

Edit: Check it in action here: http://h.kissyour.net/so/phpjs/

Edit: Edited example on my server to closer resemble what I wrote here.

Edit: Oh yes. Don't forget to clean your code!

Adam Kiss
Don't forget the quotes around `<?php echo $_GET['lang']; ?>` and the semicolon after. The semicolon is not required but will cause problems if the script is minified ;)
Tatu Ulmanen
o thanks, edited :)
Adam Kiss
I'm confused, shouldn't it be my.php.js?Are you including a php file with a javascript "src statement" (<script type="text/javascript" src="my.js.php?lang=es"></script>), is that possible?
janoChen
try it and you'll see :] and no, `my.js` says its js (for you) and `.php` tell server to parse it with php first :)
Adam Kiss
I tried it and I added 'http://localhost/tests/js-php/index.php?lang=es' to the URL, but nothing happened.
janoChen
janoChen: last 10 mins I played with quick demo - check edit ;)
Adam Kiss
You really shouldn't just be stuffing the parameter in quotes; what you really should be doing instead is using `json_encode` to put the value there. It puts the quotes in itself, and also escapes any troublesome characters to prevent XSS attacks. Example of how your code might be vulnerable to an XSS attack: `my_script.php?lang=en%22%3BdoSomethingMalicious()%3Bvar%20something%3D%22`
icktoofay
icktoofay: but it's not really point of StackOverflow to do everything for you, is it? when going live with anything, I do all the cleaning and so I advise to janoChen, but I won't write him whole code he needs :]
Adam Kiss
Thanks for the demo!
janoChen
@janoChen - you're welcome :]
Adam Kiss
A: 

In this specific case, why not simply properly set the document language and then look at that using JavaScript?

<html lang="es">

And in the script:

if (document.documentElement.getAttribute('lang') === 'es') { alert('Spanish'); }
janmoesen
A: 

You can – as the accepted answer indicates – generate entire javascript files with PHP. But if you are just trying to access some limited dynamic content, this is often overkill. If you are just trying to access a few variables that need to be PHP generated, inline javascript works fine. Add this to your HTML's head:

<script type="text/javascript">
  <?php if( $condition == true ) : ?>
    var variable1 = <?php $var1 ?>,
        variable2 = <?php $var2 ?>;
  <?php else: ?>
    var variable1 = <?php $var1Alt ?>,
        variable2 = <?php $var2Alt ?>;
  <?php endif; ?>
</script>

Just make sure you add this before any linked scripts depend on these variables.

kingjeffrey