Can anyone help me to access php variables in an external javascript file?
Thank You.
access php in javascript
Moderator: General Moderators
Re: access php in javascript
I doubt that.
You could use cookies though.
You could use cookies though.
-
DaiLaughing
- Forum Commoner
- Posts: 76
- Joined: Thu Jul 16, 2009 8:03 am
Re: access php in javascript
You could place the variables in a hidden element on the html page. It would then be available to Javascript.
Can external Javascript files be written by PHP the way external CSS can?
Can external Javascript files be written by PHP the way external CSS can?
Re: access php in javascript
Yeah that's another point.
Although, why can't you just do this?
And yes, anything can be written in PHP as long as you send the right headers.
Although, why can't you just do this?
Code: Select all
echo '<script>
var something = \''.$a_php_var.'\'; //stupid highlighter...those should be escaped
</script>';
-
DaiLaughing
- Forum Commoner
- Posts: 76
- Joined: Thu Jul 16, 2009 8:03 am
Re: access php in javascript
It was external javascript jackpf but I wonder if the variable could be passed to the function from the page.
Re: access php in javascript
Well, if you put
var something = '<?php echo $var; ?>';
and then
<script src="yourscript.js"></script>
something should then be in the scope of yourscript.js as well...so should be able to access it.
var something = '<?php echo $var; ?>';
and then
<script src="yourscript.js"></script>
something should then be in the scope of yourscript.js as well...so should be able to access it.