这可能与(a)使用Firefox或(b)该你喂encodedComponent
到,像谷歌搜索特定的API。
这里是在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
这样你就可以节省一些字母。 ;)
它在我身边正常工作。 –
对我来说,Chrome浏览器没问题,但没有Firefox。 – Needpoule
对我来说,它在两个浏览器中都可以工作。 –