I need to set JavaScript variable by php variable with out ajax. I try following code:
<script type="text/javascript">
var url = <?php echo($url); ?>;
</script>
but it will make problem,How can I do it?
asked Oct 7, 2011 at 6:38
Moein Hosseini
4,40415 gold badges73 silver badges110 bronze badges
1 Answer 1
Url is a string, so put it within quotes:
var url = '<?php echo($url); ?>';
answered Oct 7, 2011 at 6:40
zerkms
256k74 gold badges447 silver badges551 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Adam Fowler
Also, You can shorten it by doing this: <?=$url;?>
doub1ejack
@AdamF - Just remember that this is system-dependent. See stackoverflow.com/questions/200640/…
default