2012-10-09 176 views
0

我只想將基礎url傳遞給一個javaScript函數。函數調用就像如下如何將url作爲參數傳遞給JavaScript函數?

<img src="assets/images/c_arow_rite.jpg" onClick="nextMonth('.$bs_url.')"/> 

它顯示像

Error: SyntaxError: identifier starts immediately after numeric literal 
Source File: `http://mywebsite/rand/` 
Line: 1, Column: 15 
Source Code: 
nextMonth(http%3A%2F%2F192.168.1.254%2Frand%2F) 
+1

把它作爲一個字符串? – TheZ

+0

嘗試將雙引號內的URL作爲字符串傳遞。 –

+0

不要通過它? 'location.href','location.host','location.pathname','location.protocol','location.port' ...已經有很多信息可供您使用。如果它不是當前網址,那麼只有在php引號是「或heredoc afaik,而不是' –

回答

1

在錯誤控制檯的錯誤這條線:

<img src="assets/images/c_arow_rite.jpg" onClick="nextMonth('.$bs_url.')"/> 

應該寫成:

<img src="assets/images/c_arow_rite.jpg" onClick="nextMonth(\''.$bs_url.'\')"/> 

添加字符串分隔符tha不要把HTML的雙引號放在一起,並且在你的php腳本中被轉義。我想我是正確的假設這其實就像是一個語句的一部分:

echo '<img src="assets/images/c_arow_rite.jpg" onClick="nextMonth(\''.$bs_url.'\')"/>'; 

在這種情況下,加入逃過單引號是答案。

0

我想你可以做這樣的:

<img src="assets/images/c_arow_rite.jpg" onClick="nextMonth($bs_url)"/>

+0

,現在引用是什麼? –

+0

@JanusTroelsen時引用url'' – Elbek

相關問題