Echo $domain in javascript

hi

i want to replace “http://localhost/site” with “$domain” variable value


echo "<script language='javascript'>";
echo "window.location='http://localhost/site/shop/'";
echo "</script>";

vineet

Yes, and the problem is?


echo "<script language='javascript'>"; 
echo "window.location='$domain/shop/'"; 
echo "</script>";  

hi scallio

$domain already contains a slash / in the end.

so i get two slash after domain //


<?
$domain = "http://localhost/mysite/";
echo "<script language='javascript'>"; 
echo "window.location='$domain/shop/'"; 
echo "</script>"; 
?>

vineet

Ah, so then it would become $domainshop which of course doesn’t work. To avoid this you can use curly brackets to tell PHP where the var is delimited, like this:


<?
$domain = "http://localhost/mysite/";
echo "<script language='javascript'>"; 
echo "window.location='{$domain}shop/'"; 
echo "</script>"; 
?>

great scallio

it works fine.

i learned a new thing about using curly brackets this way

vineet

Are you just using this to redirect btw?

If so, this would be better:


$domain = "http://localhost/mysite/";
header("Location: {$domain}shop/", true, 301);
exit;

That way PHP will redirect and the user doesn’t need to have javascript enabled, plus Google likes this whereas it doesn’t understand the javascript redirection.

Just saying :slight_smile:

hi scallio

i used javascript redirect because i cannot echo anything before header() php

vineet

Yeah okay that makes sense :slight_smile: