1

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?

1 Answer 1

14

Url is a string, so put it within quotes:

var url = '<?php echo($url); ?>';
Sign up to request clarification or add additional context in comments.

2 Comments

Also, You can shorten it by doing this: <?=$url;?>
@AdamF - Just remember that this is system-dependent. See stackoverflow.com/questions/200640/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.