-5

My code:

var link;
var wid;
chrome.tabs.getSelected(null,function(tab) {
    link = tab.url;
});
http.open('get', 'http://surfkid.redio.de/linki.php?site_url='+link);
function insertReply() {

}
http.onreadystatechange = insertReply();
http.send(null);

This don't work, but I don't know why.

MrSmith42
  • 9,961
  • 6
  • 38
  • 49
user422039
  • 1,447
  • 4
  • 13
  • 28

1 Answers1

1

You forgot to initiate an instance of XMLHttpRequest:

var http = new XMLHttpRequest();

You need to use encodeURIComponent to properly encode your query parameter:

http.open('get', 'http://surfkid.redio.de/linki.php?site_url='+encodeURIComponent(link));

You want to attach an event listener to http.onreadystatechange, but you're actually calling insertReply and set its return value instead. Get rid of those parentheses:

http.onreadystatechange = insertReply;

UPDATE: chrome.tabs.getSelected works asynchronously, so when accessing link after the function is executed, it probably still is undefined (also see How can I get the URL for a Google Chrome tab?) Put your code in the callback handler. Complete script:

var wid,
    http = new XMLHttpRequest();

chrome.tabs.getSelected(null,function(tab) {
    http.open('get', 'http://surfkid.redio.de/linki.php?site_url=' + encodeURIComponent(tab.url));
    http.onreadystatechange = insertReply;
    http.send(null);
});

function insertReply() {

}
Community
  • 1
  • 1
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
  • now i have a db entry but the entry is: . undefined. my php code: – user422039 Nov 10 '10 at 23:35
  • @user: Please don't post code within comments, but update your question and eventually post a comment that you did so. This is hardly legible. – Marcel Korpel Nov 10 '10 at 23:42
  • sorry php --> http://codeviewer.org/view/code:13cf link.html --> http://codeviewer.org/view/code:13ce mainifest --> http://codeviewer.org/view/code:13d0 – user422039 Nov 10 '10 at 23:47
  • ohhh you fixed something link.html -->http://codeviewer.org/view/code:13d1 – user422039 Nov 10 '10 at 23:50
  • so you are my hero ^^ it works – user422039 Nov 10 '10 at 23:53
  • @user: You're welcome, nice to know it works, despite the not very clear description of your issue. Please don't use Pastebins (like codeviewer.org) to post snippets of code, but edit your question and paste your code in there, so we don't need to visit another site to view it (and mind the 4 spaces, which are inserted automatically after selecting your code and pressing the code button, the one with `010101`). – Marcel Korpel Nov 11 '10 at 00:17