-1

In my JavaScript file I've if statement and some code inside it. And I'm trying to use super global variable inside js function.

Here is the PHP code embedded inside JavaScript function:

if(typeof window.history.pushState == 'function') {
    window.history.pushState({}, "Hide", '<?php echo $_SERVER['PHP_SELF'];?>');
}

$_SERVER['PHP_SELF'] variable stands for 3-parametr of js function.

One more thing: When I write this code inside my somefile.php it's working fine. But I'm gonna put this partition of code inside external js file and include it. How can I achieve this?

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
Scott
  • 4,974
  • 6
  • 35
  • 62

1 Answers1

0

As long as you have PHP parse it - appending .php at the end of the filename, if you're using apache - it should be fine. You can load javascript files that end in PHP <script src="test.js.php" />.

Fredster
  • 776
  • 1
  • 6
  • 16
  • I'm using Nginx and I tried append js file end of my php file. But it's still not working – Scott Jul 11 '16 at 12:09
  • But it's the other way around.. append .php at the end of your .js file. E.g. `somefile.js.php` – Fredster Jul 11 '16 at 12:10
  • You mean including php file inside js file? – Scott Jul 11 '16 at 12:34
  • Yes, like you have in your question. PHP will be parsed by the server, JS will be parsed by the browser. – Fredster Jul 11 '16 at 12:39
  • I've done as you said. But nothing changed. When I inspect browser it comes error: `Uncaught SyntaxError: Unexpected token <` – Scott Jul 11 '16 at 12:56
  • Then, Nginx is not parsing the PHP file. I'm not an expert on Nginx, check this maybe: http://stackoverflow.com/questions/2394967/nginx-doesnt-parse-php-request – Fredster Jul 11 '16 at 12:59
  • 1
    Thanks man. I appreciate your help ) – Scott Jul 11 '16 at 15:07