javascript - 在 Jquery 中使用 GET 变量重新加载 URL

标签 javascript jquery

我正在尝试重新加载一个在 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.reloadreload 接受一个 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/

相关文章:

javascript - 如何使用html+javascript和node显示警告框

javascript - 使用 AJAX 从 PHP 数组填充表格内容

javascript - 发送复选框组的值

javascript - 鼠标悬停/移出事件时出现奇怪的动画行为

javascript - 显示 0-100 范围输入,最小值为 30,但仍显示输入上的 0-30 区域

javascript - 向下滚动时滚动绑定(bind)隐藏标题,反之亦然

javascript - 工具提示未显示

javascript - 添加大量输入框

javascript - owl.carousel.change 上的动画 CSS

javascript - 使用 jQuery 查看一个 div 是否有某个类的 child