Forum Moderators: open

Message Too Old, No Replies

Call perl script with javascript

         

Lectrician

9:52 pm on Nov 9, 2009 (gmt 0)

10+ Year Member



I have a perl forum running with several bits of javascript too where required.

I have one instance where I would like to have a button activate a javascript function to do a couple things browser side, which is fine.

However, this same button needs to do something with perl too. I would like the javascript to call a URL which will do this, but remain on the same page.

Is there away to 'tunnel' the javascript to call this script in the background, or do I need to pop open a window that the user then closes (message saying it completed and a link to close), or an auto closing window?

Thanks in advance.

Fotiman

9:54 pm on Nov 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could use AJAX to send an HTTP request (staying on the same page). Are you using any JavaScript frameworks, like the Yahoo UI Library or jQuery?

Lectrician

7:25 am on Nov 10, 2009 (gmt 0)

10+ Year Member



Nothing like that.

I am using ajax to pull an external page into a div, but TBH, the code for ajax I have used is a demo from the web so I am not too hot on coding anything ajax. Javascript I just about understand the basics (with help from google when needed!).

I will have a google at ajax and HTTP request. I guess it will return true if executed, and not if it fails? Is it possible to return a value outputted from the HTTP request? I could then get my perl script to return a value of 1 or something if the perl script executed correctly.

Thanks for your time.

Fotiman

2:04 pm on Nov 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It will return whatever the HTTP response is. So if you pointed a web browser at the URL of the perl page, whatever is returned is what will be returned in the AJAX request.

The reason I was asking if you were using YUI or jQuery is because these library's typically simplify AJAX requests to work cross-browser, and the interface is usually much simpler than if you were to create your own home-grown implementation.

There is a good AJAX Tutorial at w3schools [w3schools.com] that might help you understand what's going on.

Lectrician

4:49 pm on Nov 10, 2009 (gmt 0)

10+ Year Member



Thanks.

I have had a go with that tutorial, and I can make sence of it more now - I have even trimmed some unneeded code from my previous script.

I have been using two ways of doing this, both shown below, one commented out.

Which would be best?


function enableGallery(){
//var xmlhttp;
//if (window.XMLHttpRequest)
// {
// code for IE7+, Firefox, Chrome, Opera, Safari
// xmlhttp=new XMLHttpRequest();
// }
//else
// {
// code for IE6, IE5
// xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
// }

var xmlhttp = false;
if (window.XMLHttpRequest){ // if Mozilla, Safari etc
xmlhttp = new XMLHttpRequest()
}
else if (window.ActiveXObject){ // if IE
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else{
return false
}

//from here down requires one of the two above snippets.

xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4){
document.getElementById('testdiv').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET", 'my_url', true);
xmlhttp.send(null);
}

Also, I am having trouble with returning the value. Currently I return a page with '1' on it, but I can't use this as a variable in javascript as it shows as undefined.

I must point out I am a NOVICE DIYER when it comes to coding!

Fotiman

5:24 pm on Nov 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The one you are using looks better, as it will try to use the newer "Msxml2.XMLHTTP" ActiveXObject if it exists before attempting to use the "Microsoft.XMLHTTP" ActiveXObject.

When this executes, does your 'testdiv' end up with the value '1'?

Lectrician

5:32 pm on Nov 10, 2009 (gmt 0)

10+ Year Member



Yep, it does get the value 1.

I have also played around with it and got the 1 to appear as a javascript variable now, and so can execute javascript code aslong as my perl script returns a 1.

So does the second option of script (the one I am using) cover all bases as far as browsers go?

Thanks for your input, it is much appreciated.

Fotiman

6:04 pm on Nov 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, the script you're using should cover your bases.

Lectrician

8:56 pm on Nov 10, 2009 (gmt 0)

10+ Year Member



One other thing if I may.

The HTTP returned to the javascript seems to cache? This is causing me issues as my perl script is generating on the fly, and returns either a 1 or a 2. The perl script outputs to screen correctly when tested directly, but the javascript seems to cache?

Fotiman

9:30 pm on Nov 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Modify your request to include a dynamic value. For example, if you are sending a request to:

http://www.example.com/

Modify that to include some dynamic data. The current date is usually a good method.

http://www.example.com/?ck=1231231231

For example:


var url = "http://www.example.com/?ck=" + (new Date()).getTime();

(ck stands for "Cache Killer")

Lectrician

6:48 am on Nov 11, 2009 (gmt 0)

10+ Year Member



So simple!

Thanks.