2012-07-06 52 views
0

我在JavaScript中的「windows.location」命令有問題。我想在鏈接windows.location中添加php變量。我能怎麼做? 例如:我想通過變量$ LANG我們如何注入一個PHP變量到Javascript

這裏用戶轉移到英文頁面或越南頁是我的代碼

回聲「了window.location =‘/ B2C/$ LANG/confirm_fone.html’」 ;

,結果在地址欄是:

http://10.160.64.4:1234/B2C/$lang/confirm_fone.html 

的$ LANG在地址欄不能解碼?

回答

7

單引號字符串中的變量不會在PHP中進行插值。

使用這個代替:

echo 'window.location="/B2C/' . $lang . '/confirm_fone.html"'; 

或者使用雙引號:

echo "window.location='/B2C/$lang/confirm_fone.html'"; 
+0

感謝它爲我工作 – Ryan 2012-07-06 07:40:18

2

這是因爲整個字符串用單引號。

你會想要使用雙引號插入。

否則,你可以嘗試:

echo 'window.location="/B2C/'.$lang.'/confirm_fone.html"'; 
+0

謝謝 - 現在適合我 – Ryan 2012-07-06 07:40:34

0

如果你把字符串中的PHP變量,你應該使用雙引號。

echo "window.location='/B2C/$lang/confirm_fone.html'"; 
0

你來串聯值,如下所示:

echo 'window.location="/B2C/'.$lang.'/confirm_fone.html"'; 
0

的變量不是由PHP在字符串解決當您使用'爲分隔符。改爲使用"(對於javascript命令使用')或使用.連接字符串。

0

如果你是在PHP上下文:

echo "window.location=\"/B2C/"{$lang}"/confirm_fone.html\";'; 

如果你是在HTML的上下文(或更好 「外PHP上下文」):

window.location="/B2C/<?php echo $lang ?>/confirm_fone.html"; 
相關問題