Forum Moderators: open
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.
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.
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.
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!
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.
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")