我想使用以下代码使用javascript重定向页面:
var s = 'http://blahblah/' + encodeURIComponent(something);
alert(s);
window.location.href = s;
Run Code Online (Sandbox Code Playgroud)
警报显示正确的编码URL,但是当我将其传递给window.locaion.href时,它将页面重定向到错误的未编码URL。我该怎么做呢?谢谢
这可能与(a)使用Firefox或(b)您正在馈encodedComponent入的特定API (例如Google搜索)有关。
这是经过Firefox稳定测试的一种解决方案:
var clearComponent = 'flowers for my boyfriend & husband on valentines';
var encodedComponent = encodeURIComponent(clearComponent);
var googleSafeComponent = encodedComponent.replace(/%20/g,'+'); // replaces spaces with plus signs for Google and similar APIs
var completeURI = 'http://google.com/?q=' + googleSafeComponent;
window.location = completeURI;
Run Code Online (Sandbox Code Playgroud)
或全部一行:
window.location = 'http://google.com/?q=' + encodeURIComponent('flowers for my boyfriend & husband on valentines').replace(/%20/g,'+');
Run Code Online (Sandbox Code Playgroud)
window.location意味着window.location.href您可以保存一些字母。;)
| 归档时间: |
|
| 查看次数: |
11031 次 |
| 最近记录: |