By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,843 Members | 2,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,843 IT Pros & Developers. It's quick & easy.

My first Javascript, is there a better way?

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.