nym*_*ymo 7 javascript escaping spaces window.open
我正在尝试window.open使用带空格的网址:
var msg = 'Hello, world!';
var url = 'http://yoursite.com';
var link = 'http://www.twitter.com/share?text=' + msg + '&url=' + url;
window.open(link);
Run Code Online (Sandbox Code Playgroud)
运行此代码将打开一个新窗口http://twitter.com/share?text=Hello,%2520world!&url=http://yoursite.com.
会发生什么是msg中的空格转换为%20,然后'%'转换为%25.作为一种解决方法,我补充说:
msg = msg.replace(/\s/g, '+');
但是我需要注意其他字符还是有更好的解决方法?
小智 11
试试这个:
var msg = encodeURIComponent('Hello, world!');
var url = encodeURIComponent('http://www.google.com');
var link = 'http://twitter.com/intent/tweet?text=' + msg + '&url=' + url;
window.open(link);
Run Code Online (Sandbox Code Playgroud)
请注意不同的Twitter网址和查询字符串参数的编码.
你必须对 URL 进行编码。
URL 中不能有任何空格。
因此,浏览器会根据需要重新解释 url 空格,除非您确切地告诉它如何:
var msg = 'Hello,%20world!';
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19008 次 |
| 最近记录: |