尝试类似以下的代码:
location.replace('http://test.com/sometest.html?myid=5&someotherid=6');
location.href = 'http://test.com/sometest.html?myid=5&someotherid=6';
var newUrl = "http://test";
window.location = newUrl;
newUrl += "?myid=" + myid;
function generateUrl(url, params) {
var i = 0, key;
for (key in params) {
if (i === 0) {
url += "?";
} else {
url += "&";
}
url += key;
url += '=';
url += params[key];
i++;
}
return url;
}
window.location = generateUrl("http://test",{
myid: 1,
otherParameter: "other param value"
});
注意:此功能仅适用于参数对象中的整数/布尔/字符串变量。不能以此方式使用对象和数组。
如果你只想调用它,而且不需要跳转到它,可以使用 AJAX 请求:
$.ajax({
url: 'http://test/?myid=5'
});
这里使用jQuery。但是互联网上有足够多的非jQuery示例。
你只需在查询字符串中正常包含它:http://test?myid=5&otherarg=3
var myid = 5;
window.location = "http://test?myid=" + myid;