Site Navigation
VegUiSiteKit - Remote Scripting with the VSK Tunnel
written by vegu on 24 Nov, 2007 07:16:22
Sometimes an xml request based solution won't suffice for a project. If, for example, you need to request data from a remote server then ajax is very limited due to browser security settings. You would always need to use server based software as a middle man for the requests.
The VSK Tunnel object uses both the IFRAME and SCRIPT elements to provide a means to send requests to remote locations.
The iframe portion of the library is very unfinished and untested, so this tutorial will focus mainly on remote scripting via the SCRIPT element.
So let's get started.
First we will need to create the script that we want to request. This is an ordinary php file that will print some basic javascript code. We will call this file test.php.
print "alert('Hello ".$_GET['name']."');";
Alright, easy enough.
Now in the file that will make the request we create a new VSK_Tunnel object and make use of it's request() method. The request() method includes the submitted url via the SCRIPT element while the send() method querys the submitted url by creating an iframe element and changing its location to it. We wont bother with send() right now :)
var Tunnel = new VSK_Tunnel();
Tunnel.request('test.php?name=bob', null, false);
The first argument of the request() function call is the url to request. The second argument is the function that should be executed after the request was successfully finished, this is optional. The last argument defines whether the request should happen in sync with the current code execution or asynchronus (like AJAX).
If everything worked correctly on loading the page that requests test.php a javascript alert message should popup with the text "hello bob".
This is very powerful as the server response itself will be javascript that will be executed. So you send a request to the server, the server does it's thing and then outputs the response in javascript and the browser processes it right away.
And unlike the XMLHttpRequest approach this metho
Related Posts
Your Comment
Comments
No comments yet.