Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what are they doing in line 3

Status
Not open for further replies.

wolf73

Programmer
Feb 12, 2006
93
CA
Hi, I don't understand what they doing in line 3, I did not know one can create object in javascript. and then why they have return with in if statament?


function package_validate()
{
if ($('package_offer_id').value != "" && valid_offer == false)
{
var url = '/integration/ajax_validate_offer_id';
var pars = 'offer_id=' + $('package_offer_id').value;
var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: validate_offer_id} );
return;
}


submit_package();

}
 
This snippet of code requires the prototype library (a javascript library) to be loaded first. It offers some facades to make javascript code less verbose.

By doing return, the "submit_package()" method will not be called if the test condition is true.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
thanks, on this code

var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: validate_offer_id} );


They are making an object myAjax, but thats it, they are not using that object. Like normaly we do in other OOP
myAjax->callfunction(); etc
 
It is normal [smile] - you may be unfamiliar with it because it's using prototype.js.

A new XMLHTTP request object is being instantiated - there are two main variables involved: url which is the url that the ajax request is to be sent to, and a javascript object. This object is broken into three parts - three variables... method which determines if the AJAX call is to use GET or POST, parameters - which includes a (escaped) string of field/value pairs, onComplete - which is the callback function to run when the AJAX call completes. This is assigned to myAjax - a handle to the XMLHTTP request object.

As I said before - it's a facade provided by prototype to cut down the amount of code you need to do more complex tasks.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
They are making an object myAjax, but thats it, they are not using that object. Like normaly we do in other OOP

Yes, they are using it. They instantiate it and use it in one call. Perhaps you should download and view the prototype JS framework - if you've got the code you can view, you might understand it a bit more.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
For completeness, you should be escaping the field/pair values string:
Code:
var pars = [COLOR=red]escape([/color]'offer_id=' + $('package_offer_id').value[COLOR=red])[/color];
Of course you won't notice any problems until you attempt to sent a space or some other "escapeable" character as part of your pars variable.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top