【问题标题】:Update querystring in javascript在javascript中更新查询字符串
【发布时间】:2014-10-24 08:49:32
【问题描述】:

我有一个使用 ajax 读取 xml 文件的网页。 Ajax 因为我只想更新数字而不是整个页面。 它使用 SetInterval() 每 5 秒读取一次文件,因为文件每 5 秒更新一次。 我想通过在查询字符串中添加一个计数器来知道 xml 文件重新加载了多少次。 我失败了,因为如果我更改查询字符串,页面会重新加载。是否可以在 JavaScript/JQuery 中做到这一点?

这就是我试图解决这个问题的方法:

function loadXMLDoc(filename) {
var xmlhttp;

if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
}
else // code for IE5 and IE6
{
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

var href = window.location.href;

if (counter < 1) {
    href += "index.html?count=" + ++counter;
    window.location.href = href;
} else {
    href = href.substring(0, href.length - 1);
    window.location.href = href + ++counter;
}

xmlhttp.open("GET", filename + "?count=" + counter, false);
xmlhttp.send();

var xdoc = xmlhttp.responseXML;

var xmlDoc = new ActiveXObject('Msxml2.DOMDocument.6.0');
xmlDoc.loadXML(new XMLSerializer().serializeToString(xdoc));

return xmlDoc;

这也不会产生我想要的查询字符串:

http://localhost:52646/index.html?count=1index.html?count=1

这可能实现吗?

【问题讨论】:

标签: javascript jquery html ajax request.querystring


【解决方案1】:

您可以使用 HTML5 localstorage 来实现这一点,下面是一些代码供您参考:

if(localStorage.getItem('count')=='undefined')
    localStorage.setItem('count',1);
else
    localStorage.setItem('count',parseInt(localStorage.getItem('count'))++);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-29
    • 2012-04-04
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多