views:

76

answers:

2

Hello. I am programming a desktop application in Delphi. So far I've been using TWebBrowser component for 1) loading a page completely and 2) THEN sending Javascript to it (Webbrowser.Navigate('javascript: join_game()')

But I don't really like that, because I have to wait for the TWebBrowser to completely load the page. I don't use it for anything else. The routine looks like this:

repeat  begin
     SourceCode:=HTTPGetText(PAGE_URL);
     // now parse the code and depending on the result either call js: join_game() or js: leave_game()
end;

Whenever either of those functions is called, the server adds or removes me, as the one currently logged on (automatically, saved in cookies) to/from a lobby.

What I am asking is, is it somehow possible to call the JS script without loading the page, something like writing this to an address bar: http://www.pagewithjs.com/javascript:function()

A: 

If your function is small enough, then you can simply put it inside the URL. Say, for example, that you have the following function:

function foo(text) {
    var a = text.split(/\W+/g);
    return a.length;
}

and you want to call it like this: foo('hello world'), then you can write this to an address bar:

javascript:(function(text){var a=text.split(/\W+/g);return a.length;})('hello world')

Note that you should have parenthesis around the function body as well: (function(){}) which causes it to be treated as a function pointer.

Note also that different browsers have different limitations on the length of an URL. If I recall correctly, then IE has the lowest limit, somewhere near 2048 characters.

HTH.

Roy Sharon
Thanks, but that is not what I meant. The JS function is stored at the server and I need the server to perform certain action by that call.
Magicmaster
A: 

The Javascript code is in the HTML page (or in a script file referenced in the HTML) so it is necessary to load the page before the code can be executed.

The standard HTML protocol does not provide a way to add a "script invocation parameter" to the URL.

Only if the script simply sends a HTTP request to the server, it might be possible to simulate this request from the HTML client.

mjustin
Crap. Well, I guess I will have to go with that not-so-elegant style. Thanks anyway.
Magicmaster