Re: JavaScript and Perl Subroutine
by diotalevi (Canon) on Aug 11, 2004 at 20:03 UTC
|
JavaScript runs on the web browser's computer, perl runs on the web server. To communicate from the browser to the server you'll need to put a CGI which can run this function on command. Perhaps you'd do something like document.location = document.location.href.replace( '/.*', '/cgi-bin/allExaminations.pl?action=logout' ); in your page's JavaScript and then you have to have your CGI be able to handle the parameter you just gave it. So maybe you said use CGI; my $q = CGI->new earlier and can then say logout() if $q->param( 'action' ) eq 'logout'. | [reply] [d/l] [select] |
Re: JavaScript and Perl Subroutine
by bgreenlee (Friar) on Aug 11, 2004 at 22:48 UTC
|
This isn't exactly what the poster was asking for, but this reminded me of something terribly cool I ran across recently:
http://developer.apple.com/internet/webcontent/XMLHttpRequestExample/example.html
I didn't know you could get JS to fetch data dynamically from the server. Works in at least Firefox and IE.
Anyway, I haven't tried yet, but you might be able to use this to request data from a CGI, although for the original poster's purposes, that's unnecessary. Just have javascript set the value of a hidden field and have the script that handles the submit direct itself to the proper sub based on that value.
| [reply] |
|
|
Ive used this technique recently for a project at work. It works great. It's a little bit to wrap you head around the first time but once you do it's pretty easy to implement.
| [reply] |
Re: JavaScript and Perl Subroutine
by amw1 (Friar) on Aug 11, 2004 at 20:11 UTC
|
While you can't do it directly you can play some games with javascript and your perl code to effect a similar type thing
This can be done without the javascript by setting the name of the logout
button to something you can grab in your script. This approach allows you to
do this for stuff other than buttons though. (i.e. changing a value in a
dropdown etc)
HTML Stuff
<script language="JavaScript">
function logout
{
var dynamicInput = document.createElement("INPUT");
dynamicInput.type = "hidden";
dynamicInput.name = "action_logout";
document.main_form.appendChild(dynamicInput);
document.main_form.submit();
}
</script>
<form method=POST action=allExaminations.pl name="main_form">
<input type=submit name=logout value=logout onClick="logout()">
</form>
Perl Stuff inside allExaminations.pl
my $q = new CGI;
#
# Look for an action value and return it
#
sub GetAction
{
my $q = shift();
my $cgi_parms = $q->Vars();
foreach my $key (keys(%$cgi_parms))
{
if ($key =~ /^action/)
{
return $key;
}
}
}
if(GetAction($q) =~ /action_logout/)
{
DoLogoutSub();
}
not tested etc.
| [reply] [d/l] |
Re: JavaScript and Perl Subroutine
by Aristotle (Chancellor) on Aug 11, 2004 at 21:20 UTC
|
The Javascript code runs on the client; the Perl script runs on the server. You can't directly do what you want.
You'll have to put the subroutines you want to call in a CGI script accessible from your webserver, and have the client request the appropriate URL in order for the CGI script to be run.
This is too much to just explain in a single reply. You'll need to look at a tutorial on writing CGI scripts. You should take a look at Ovid's excellent CGI course.
Makeshifts last the longest.
| [reply] |
Re: JavaScript and Perl Subroutine
by dragonchild (Archbishop) on Aug 11, 2004 at 20:21 UTC
|
If you can guarantee that you will only ever be using Internet Explorer, take a look at the ActiveX extension called PerlScript that ActiveState has released. (If you download ActivePerl, you automatically get it.) The documentation is very spotty, but you can do some really neat things.
Note - this will require that anyone using your website must have ActivePerl installed. But, if you do so, you can actually replace all your JavaScript with PerlScript.
Note further - PerlScript is not a complete version of Perl. There are some edge cases and dark corners you may not be able to do. But, you will have all the major capabilities of Perl. (Basically, if you don't know what I'm referring to, don't worry about it. I played with PerlScript on and off for a few months and didn't find much that it couldn't do.)
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
I shouldn't have to say this, but any code, unless otherwise stated, is untested
| [reply] |
Re: JavaScript and Perl Subroutine
by borisz (Canon) on Aug 11, 2004 at 20:01 UTC
|
You can not do that. javascript is on the clientside while the perlscript is on the server.
| [reply] |
Re: JavaScript and Perl Subroutine
by LTjake (Prior) on Aug 12, 2004 at 13:54 UTC
|
Though i can only recommend it for controlled situations, you might try my JavaScript-RPC module.
If you take a look at the demo, you can see that when you press submit (NOTE: you can't actually run the example from search.cpan.org -- it must be installed on your own webserver) it calls a perl sub to add or subtract two numbers. A dumb example, but it's the concept that matters. :)
-- "Go up to the next female stranger you see and tell her that her "body is a wonderland."
My hypothesis is that she’ll be too busy laughing at you to even bother slapping you." (src)
| [reply] |
|
|
Thank you all monks.I am reviewing your replies and will work on them.
Well my working environment is IIS,CGI-bin, Perl so now depending on this if you have got more efficient solutions please let me know.
Thanks
blazix
| [reply] |