为什么本地存储在javascript之后最后保存

发布于 2024-10-04 22:56:05 字数 3278 浏览 0 评论 0原文

我目前正在尝试在程序加载时将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扮仙女 2024-10-11 22:56:05

首先,您的代码很难阅读,请调整您的编码风格,例如:

  1. 每个缩进使用超过 1 个空格
  2. 不要将内容与左大括号和右大括号放在同一行 在
  3. 周围使用空格==!= 等。

这将使您的代码更具可读性:)

但是关于您的问题:

// 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< 的其余部分/code> 函数进入回调。

First off, your code is quite hard to read, please work on your coding style, for example:

  1. Use more than 1 space for per indent
  2. Don't place stuff on the same line with opening and closing curly braces
  3. Use spaces around ==, != etc.

This will make your code a lot more readable :)

But on to your problem:

// 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);

You most likely need to move the rest of the loaded function into the callback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文