![](/img/trans.png)
[英]Passing Javascript variable to '<a href' with 'window.location.href' in a frame
[英]Javascript passing an encoded url to window.location.href
我想使用以下代碼使用javascript重定向頁面:
var s = 'http://blahblah/' + encodeURIComponent(something);
alert(s);
window.location.href = s;
警報顯示正確的編碼url,但是當我將其傳遞給window.locaion.href時,它將頁面重定向到錯誤的未編碼url。 我該怎么做呢? 謝謝
這可能與(a)使用firefox或(b)您encodedComponent
提供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;
或全部一行:
window.location = 'http://google.com/?q=' + encodeURIComponent('flowers for my boyfriend & husband on valentines').replace(/%20/g,'+');
window.location
表示window.location.href
因此您可以保存一些字母。 ;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.