为什么本地存储在javascript之后最后保存
我目前正在尝试在程序加载时将 tab.url 保存到 html5 本地存储中,然后根据规则将 css 文件更新到标头中,这些都很容易做到。但由于某种原因,popup.html 加载前一个本地存储变量,而不是最近的本地存储变量。
我想知道是否有人可以帮助我,我使用的代码是这样的;
<script language="javascript">
var rule;
var links;
var search;
function loadcssfile(filename, filetype)
{
if (filetype == "css")
{
var fileref=document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", filename);
}
if (typeof fileref != "undefined")
{
document.getElementsByTagName("head")[0].appendChild(fileref);
}
}
function loaded()
{
chrome.tabs.getSelected(null,function(tab)
{
var temp;
temp = tab.url;
localStorage['tab'] = temp;
console.log(temp);
});
links = localStorage['tab'];
rule = localStorage['ok0'];
search = links.indexOf(rule);
if(search != -1)
{
loadcssfile("./css/styles.css","css");
loadcssfile("./button/styles2.css","css");
}
else
{
// or load other css file
}
}
document.getElementById('datacontent').innerHTML = rule + "<br>" + links + "<br>" + search;
function createXMLHttpRequest()
{
if (window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function createXMLHttpRequest2()
{
if (window.ActiveXObject)
{
xmlHttp2 = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xmlHttp2 = new XMLHttpRequest();
}
}
function handleStateChange1()
{
if(xmlHttp.readyState == 4 && xmlHttp.status == 200 )
{
var data;
data = xmlHttp.responseText;
document.getElementById('container').innerHTML = data;
}
}
function senddata(id)
{
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange1;
xmlHttp.open("GET", "http://weblinkchecker.forum-source.tk/crawler.php?link=" + links.replace('&','0123456789abcdef'), true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send("&type=anythinghere&anothervar=datahere");
document.getElementById(id).innerHTML = "Successful !";
}
function handleStateChange2()
{
if(xmlHttp2.readyState == 4 && xmlHttp2.status == 200 )
{
var data;
data = xmlHttp2.responseText;
document.getElementById('container2').innerHTML = data;
}
}
function sendForLinks()
{
createXMLHttpRequest2();
xmlHttp2.onreadystatechange = handleStateChange2;
xmlHttp2.open("GET", "http://weblinkchecker.forum-source.tk/links.php?link=" + links.replace('&','0123456789abcdef'), true);
xmlHttp2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp2.send("type=anythinghere&anothervar=datahere");
}
window.onload = function()
{
loaded();
}
</script>
正如你所看到的,它是一个网络链接检查器,有一些我没有上传的 css,还有很多使用 ajax 的 php,但这些都没有对代码产生影响,而且我还删除了任何 insideHTML 函数。抱歉有这么大的代码。
如果有人能告诉我为什么 html5 本地存储最后保存,那将是一个巨大的帮助。我尝试回显数据,但它只给出旧数据,而不是最新数据。我觉得我遇到了这个问题。
Im currently trying to save the tab.url into html5 local storage when the programme loads then update a css file into the header based on a rule, which are all easy to do. But for some reason the popup.html loads the previous local storage variable and not the recent one.
I was wondering if anyone can help me on this, the code that im using is this;
<script language="javascript">
var rule;
var links;
var search;
function loadcssfile(filename, filetype)
{
if (filetype == "css")
{
var fileref=document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", filename);
}
if (typeof fileref != "undefined")
{
document.getElementsByTagName("head")[0].appendChild(fileref);
}
}
function loaded()
{
chrome.tabs.getSelected(null,function(tab)
{
var temp;
temp = tab.url;
localStorage['tab'] = temp;
console.log(temp);
});
links = localStorage['tab'];
rule = localStorage['ok0'];
search = links.indexOf(rule);
if(search != -1)
{
loadcssfile("./css/styles.css","css");
loadcssfile("./button/styles2.css","css");
}
else
{
// or load other css file
}
}
document.getElementById('datacontent').innerHTML = rule + "<br>" + links + "<br>" + search;
function createXMLHttpRequest()
{
if (window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function createXMLHttpRequest2()
{
if (window.ActiveXObject)
{
xmlHttp2 = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xmlHttp2 = new XMLHttpRequest();
}
}
function handleStateChange1()
{
if(xmlHttp.readyState == 4 && xmlHttp.status == 200 )
{
var data;
data = xmlHttp.responseText;
document.getElementById('container').innerHTML = data;
}
}
function senddata(id)
{
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange1;
xmlHttp.open("GET", "http://weblinkchecker.forum-source.tk/crawler.php?link=" + links.replace('&','0123456789abcdef'), true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send("&type=anythinghere&anothervar=datahere");
document.getElementById(id).innerHTML = "Successful !";
}
function handleStateChange2()
{
if(xmlHttp2.readyState == 4 && xmlHttp2.status == 200 )
{
var data;
data = xmlHttp2.responseText;
document.getElementById('container2').innerHTML = data;
}
}
function sendForLinks()
{
createXMLHttpRequest2();
xmlHttp2.onreadystatechange = handleStateChange2;
xmlHttp2.open("GET", "http://weblinkchecker.forum-source.tk/links.php?link=" + links.replace('&','0123456789abcdef'), true);
xmlHttp2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp2.send("type=anythinghere&anothervar=datahere");
}
window.onload = function()
{
loaded();
}
</script>
As you can see its a web link checker, there is a bit css that I didn't upload, also there a lot of php using ajax but none of that is having an affect on the code and I also removed any innerHTML functions. Sorry for having such a large piece of code.
if any one could tell me why html5 local storage is saving last it would be a huge help. I tried echoing out data and it just gives out the old data and not the recent. I feel like I hit a brick wall with this one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您的代码很难阅读,请调整您的编码风格,例如:
周围使用空格==
、!=
等。这将使您的代码更具可读性:)
但是关于您的问题:
您很可能需要移动
loaded< 的其余部分/code> 函数进入回调。
First off, your code is quite hard to read, please work on your coding style, for example:
==
,!=
etc.This will make your code a lot more readable :)
But on to your problem:
You most likely need to move the rest of the
loaded
function into the callback.