我目前正在尝试在程序加载时将 tab.url 保存到 html5 本地存储中,然后根据规则将 css 文件更新到 header 中,这些都很容易做到。但由于某种原因,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 本地存储最后保存,那将是一个巨大的帮助。我尝试回显数据,但它只给出旧数据,而不是最新数据。我感觉我遇到了这个问题。
最佳答案
首先,您的代码很难阅读,请注意您的编码风格,例如:
- 每个缩进使用超过 1 个空格
- 不要将内容与左大括号和右大括号放在同一行
- 在
==
、!=
等周围使用空格。
这将使您的代码更具可读性:)
但是关于你的问题:
// this looks like a callback....
chrome.tabs.getSelected(null,function(tab) {
var temp;
temp = tab.url;
localStorage['tab'] = temp;
console.log(temp);
});
// this will most likely get executed BEFORE the callback gets called
// therefore the values just hasn't been changed yet
links = localStorage['tab'];
rule = localStorage['ok0'];
search = links.indexOf(rule);
您很可能需要将 loaded
函数的其余部分移至回调中。
关于javascript - 为什么本地存储在javascript之后最后保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4293782/