我正在尝试重新加载一个在 URL 中附加变量 deviceID 的页面。它显示在“实时”div 中就好了。我做错了什么?:
document.getElementById("realtime").innerHTML = "<pre>"+JSON.stringify(msg)+"</pre>";
var obj = jQuery.parseJSON(JSON.stringify(msg));
var deviceId = obj.deviceId;
var pathname = window.location.pathname;
var pathAppend = pathname + "?deviceId=" + deviceId;
window.location.reload(pathAppend);
最佳答案
这是因为你错误地使用了window.location.reload
。 reload
接受一个 bool 值:
when it is true, causes the page to always be reloaded from the server. If it is false or not specified, the browser may reload the page from its cache. - MDC
你应该这样做:
var query = window.location.search, deviceParam = "deviceId=" + deviceId;
//if there is a query string, append it, otherwise construct the query string.
query += (query === "" ? "?" : "&") + deviceParam;
window.location.search = query; // page should reload
关于javascript - 在 Jquery 中使用 GET 变量重新加载 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3762419/