2016-02-10 44 views
2

有一個頁面上有「share at whatsapp」按鈕。這將創建並執行一個URL,如:使用location.href或類似的方法打開一個URL後跟一個散列符號

的WhatsApp://發送文本=寫一些文字,然後鏈接 - http://link_to_this_page#something

的問題是,瀏覽器(我?現在只用Chrome進行測試)會自動從哈希標記中刪除。

我已經試過了基本的:

var href = 'whatsapp://send?text=Example text - '; 
var uri = location.protocol + '//' + location.host + location.pathname + '#gm.'; 
location.href = href + uri; 

location.replace()location.assign()window.open()沒有運氣嘗試過。

所以問題是,我該怎麼辦?使用哈希是必要的,因爲它告訴目標頁面它必須在javascript中做一些事情(這可能需要更多時間來改變)。

+0

'document.location.href = 「你的網址」 + window.location.hash' – xdevs23

回答

2

您應該編碼查詢字符串中的任何內容。

location.href = href + encodeURIComponent(uri); 

你或許應該這樣做:

var href = 'whatsapp://send?text='; 
var text = 'Example text - '; 
var uri = location.protocol + '//' + location.host + location.pathname + '#gm.'; 
location.href = href + encodeURIComponent(text + uri); 
+0

哦,當然!我沒有意識到這一點。謝謝你,小夥伴。 – kosmos

相關問題