The following reads data from a json file generated by a database call.
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'next.php', true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
callback(JSON.parse(xobj.responseText));
}
};
xobj.send(null);
}
The following uses the above function to select a random url and change the url when user clicks button.
var page = '';
var nextpageButton = document.getElementById('nextpage');
nextpageButton.addEventListener('click', function() {
loadJSON(function(json) {
var limit = json.length;
var i = ((Math.floor(Math.random()*limit)+1) - 1);
page = (json[i].sites);
chrome.tabs.query({currentWindow: true, active: true}, function (tab) {
chrome.tabs.update(tab.id, {url: page});
});
});
});
Both above functions work perfectly.
The following adds data to databse when the user clicks button. This works perfectly as long as I don’t add a nested XMLHttpRequest on success.
var like = document.getElementById('tu');
like.addEventListener('click', function() {
var select_element = document.getElementById('cat');
var cat = select_element.value;
if (cat != 0) {
chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
var current_url = tabs[0].url;
var current_title = tabs[0].title;
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "add.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("sites="+current_url+"&title="+current_title+"&vote_up=1&vote_down=0");
});
} else {
select_element.focus();
}
});
Things start to break when I add the following to the above code:
//this is the last row from above code before adding new code
xhttp.send("sites="+current_url+"&title="+current_title+"&vote_up=1&vote_down=0");
//this is new code
xhttp.onreadystatechange = function() {
if(xhttp.readyState == 4 && xhttp.status == 200) {
loadJSON(function(json) {
var limit = json.length;
var i = ((Math.floor(Math.random()*limit)+1) - 1);
page = (json[i].sites);
chrome.tabs[0].update(tab.id, {url: page});
});
}
}
When I add the above code, which should just be calling the first function, I start getting javascript error Cannot read property 'url' of undefined
.
Se the question is… if javascript reads in a linear manner from top to bottom, why would the property become undefined with the addition of the loadJSON(function(json)
? I can remove that function and everything works again. This is not a variable that is defined by the XMLHttpRequest so why would a nested request stop the variable from being loaded correctly?
More important how do I fix this?
advair diskus
how to order lasix online
prednisone purchase canada
cytotec for sale in cebu
I absolutely love your site.. Very nice colors & theme.
Did you develop this site yourself? Please reply back as I’m looking to create my own personal blog and would like to learn where
you got this from or just what the theme is called.
Thanks! https://hhydroxychloroquine.com/
I’m really impressed with your writing skills as well as with the layout on your blog.
Is this a paid theme or did you modify it yourself?
Anyway keep up the nice quality writing, it is rare to see
a nice blog like this one nowadays. http://www.deinformedvoters.org/hydroxychloroquine
cialis no prescription canada – 4467 cialis cialis once a day
Greetings! Very useful advice in this particular post!
It is the little changes which will make the most significant changes.
Many thanks for sharing! http://cleckleyfloors.com/
order zithromax 100mg – azithromycin price of zithromax
I needed to thank you for this good read!! I absolutely loved every bit of it.
I’ve got you book-marked to check out new things you post https://herreramedical.org/hydroxychloroquine
200mg sildenafil soft gel capsule – buy viagra online australia fast delivery viagra viagra 25mg
buy ventolin online nz
prozac cost
80 mg prozac
z pack for ear infection in adults
Excellent article. I will be facing many of these issues as
well.. http://hydroxychloroquined.online/
dapoxetine tadalafil combination in india
super kamagra (sildenafil with dapoxetine)
2 year old swallowed lisinopril
sildenafil citrate tablets vs viagra
how to buy propecia uk
methylprednisolone 16mg – methylprednisolone 4mg tablet lyrica without rx
lasix for horses for sale
trusted website to buy clomid
order prednisolone online
advair online coupon
buy hydroxychloroquine – hydroxychloroquine sulfate over the counter prednisone over the counter south africa
viagra pills online canada – canadian viagra sildenafil 50mg for sale
554816 911289Wow, incredible weblog format! How lengthy have you been blogging for? you make running a blog glance effortless. The full glance of your website is amazing, as smartly the content material material! 146116
pay for a research paper – academicwriting academia writers
prednisone 200 mg price – prednisone pill can you buy prednisone in canada
casino games win real money – slots games free best online casino
ivermectin treatment stromectol 3mg tablets tractor supply ivermectin pour on how effective is ivermectin for scabies
purchase sildalis without prescription – order metformin 1000mg glucophage order online
buy ampicillin 500mg online cheap – hydroxychloroquine pills order hydroxychloroquine generic
purchase hydroxychloroquine online cheap – buy hydroxychloroquine pills buy plaquenil 200mg for sale