我正在尝试在程序加载时将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>
正如你可以看到它的一个web链接检查器,有一些我没有上传的css,也有很多使用ajax的php,但没有一个对代码产生影响,我也删除了任何innerHTML功能。很抱歉有这么大的代码。
如果任何人可以告诉我为什么html5本地存储最后保存它将是一个巨大的帮助。我尝试回显数据,它只是提供旧数据而不是最近的数据。我觉得我用这个砖打了一堵砖。
答案 0 :(得分: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
函数的其余部分移动到回调中。