Javascript将编码的url传递给window.location.href

Javascript将编码的url传递给window.location.href,javascript,Javascript,我想使用以下代码重定向带有javascript的页面: var s = 'http://blahblah/' + encodeURIComponent(something); alert(s); window.location.href = s; 该警报显示正确的编码url,但当我将其传递到window.locaion.href时,它会将页面重定向到错误的未编码url。 我怎样才能做好呢? 谢谢这可能与(a)使用firefox或(b)您输入的encodedCompone

我想使用以下代码重定向带有javascript的页面:

    var s = 'http://blahblah/' + encodeURIComponent(something);
    alert(s);
    window.location.href = s;
该警报显示正确的编码url,但当我将其传递到window.locaion.href时,它会将页面重定向到错误的未编码url。 我怎样才能做好呢? 谢谢

这可能与(a)使用firefox或(b)您输入的
encodedComponent
特定API有关,如谷歌搜索

以下是一个在Firefox stable上经过测试的解决方案:

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
可以保存一些字母

它在我这边运行得很好。对我来说,它在chrome上运行得很好,但在firefox上不行。对我来说,它在两种浏览器上都可以运行。实际上,它在两种浏览器上都可以运行。但是firefox在url栏中显示了未编码的url。听起来像是版本问题!出于兴趣,您能告诉我们哪些版本的浏览器不适合您吗?