Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top