views:

38

answers:

1

Hey everyone,

I'm fairly bad at javascript, but here's what I want. I have a PHP script on my server, and when a user presses my Safari Extension button, I want a request (url) sent to my server (www.myurl.com/directory/script.php?etc), but I don't really want a new window/tab opening to load it. Is there a way load it in a hidden window and close it when done or something like that, either with the Safari API or straight javascript?

Thanks guys!

+2  A: 

Straight Javascript has XMLHttpRequest, which performs a background, asynchronous by default HTTP request.

The documentation link points to Mozilla's, though it works the same with Safari.

var request = new XMLHttpRequest();
request.open("GET", "http://www.example.com/mypage");
request.onreadystatechange = function()
{
    // "4" means "completed"
    if (request.readyState != 4) return;

    // do something with the response from the server
    alert(request.responseText);
}
request.send();

Safari extensions can't do cross-domain requests from an injected script. For instance, if your user visits google.com, you cannot send a request to example.com from the injected script because the domains don't match. (Actually, the protocols and the ports must match, too.)

This means that you'll need your global page to perform the requests, and your injected scripts will have to do message passing to it. There's a good deal of documentation available on this on the Safari Developer Center.

zneak
Awesome! That worked great!
Brian515