My first Javascript, is there a better way?

S

smorrey

I decided for my first Javascript program, I would port a php currency
conversion utility. However I've come to find out that something
critical was missed during the design phase. That is that
XMLHttpRequest does not function in this circumstance at all, I keep
getting permission denied errors (I'm working in Firefox). Would
someone mind taking a look at the following script and letting me know
if there was maybe a better way, or can this even be done in
Javascript?

Thanx in advance for the help.

function loadXMLDoc(url){
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send(null);
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send();
}
}
}

function processReqChange(){
// only if req shows "complete"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
// ...processing statements go here...
response = req.responseXML.documentElement.data;
alert("Response was recieved!:\n");
eval(checkRate + '(response)');
} else {
alert("There was a problem retrieving the rate data:\n" +
req.status);
}
}
}


function checkRate(response)
{
alert("checkRate was entered!:\n");
if (response){
// Response mode
alert("Response was not NULL, response is :\n" + response);
if (response == '1'){
message.className = 'error';
}else{
fromprice = document.getElementById('currency_amount').value;
price = fromprice * response;
fromprice.value = price;
}
}else{
// Input mode
fromcur = document.getElementById('currency_from').value;
tocur = document.getElementById('currency_to').value;
url =
'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=l1&s='+fromcur+tocur+'=X';
alert("url is :\n" + url);
loadXMLDoc(url);
}

}
 
J

Janwillem Borleffs

I decided for my first Javascript program, I would port a php currency
conversion utility. However I've come to find out that something
critical was missed during the design phase. That is that
XMLHttpRequest does not function in this circumstance at all, I keep
getting permission denied errors (I'm working in Firefox). Would
someone mind taking a look at the following script and letting me know
if there was maybe a better way, or can this even be done in
Javascript?

Without special security measurements, this can only work when the request
is performed on the same server. One of these measurements consists of
signing your scripts. See the following page for more information:

http://www.mozilla.org/projects/security/components/signed-scripts.html

An easier solution, however, would be to have a local PHP script retrieve
the content from Yahoo, afterwhich you can parse its response with
JavaScript.


JW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top