469,647 Members | 1,800 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

My first Javascript, is there a better way?

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);
}

}

Oct 16 '05 #1
1 1068
sm*****@gmail.com wrote:
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/secu...d-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

Oct 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Sergio del Amo | last post: by
136 posts views Thread by Matt Kruse | last post: by
14 posts views Thread by Sinity | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.