5 Replies - 692 Views - Last Post: 25 August 2010 - 06:56 AM

#1 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 23
  • View blog
  • Posts: 557
  • Joined: 04-August 09

I need to know what this code does

Posted 24 August 2010 - 08:53 PM

I'm trying to convert a web application into a desktop application for my self learning

the web code is in javascript/ajax

I have experience with javascript before but don't know about ajax

so here is a piece of the code that I didn't understand what is its algorithm

the original code is very long and i managed with my experience in javascript to convert it

but this one I couldn't figure it out

function check()
{
	if (!checking)
	{
		id = getId();
		if(!isValidId(id))
			return;
		checking = true;
		
		$.ajax({
			url: '/detectinvisible2.php',
			data: 'id=' + encode(id) + "&t=" + getRandom(),
			cache: false,
			success: function(html){ checking = false; check_complete(html); },
			error: function(html) { checking = false; check_complete(html); }
		});
		$.ajax({type:"GET",url:"hi5.php?id="+yid+"&t="+Math.random()});
	}
	else { alert('Please wait until you get the response.'); }
}

function check_complete(html)
{
	var rand_no = Math.ceil(1000*Math.random());
	var status = 'error';
	var col = 'd84536';
	var msg = '';
	var splitted = html.split('|');
	var code = splitted[0];
	if (code == 'undefined') code = '99';
	yid = splitted[1];
	if (id == 'undefined') id = '';
	var ylink1 = ylink2 = ylink3 = ylink4 = ylink5 = '';
	var opt1 = '<a style="margin-left: 15px; float: right; display: block" rel="nofollow" href="#" onclick="javascript:$(\'#ypanel\').hide();">Close [x]</a>';
	var opt2 = '';
	switch (code){
		case '6': window.location = "/Unban.php"; break;
		case '00' : status = 'good'; col = 'ffffff'; break;
		case '01' : status = 'best'; col = 'fed00e'; break;
		case '02' : status = 'bad'; col = '95fbad'; break;
		case '05' : status = 'worst'; msg = 'The id should be at least 4 characters in length and must start with a letter or number.'; break; 
		case '06' : status = 'servers busy'; msg = 'Our servers are busy. Please try again later.'; break;
		case '07' : status = 'maintenance'; msg = 'We are doing a short maintenance. Please try again later.'; break;
		case '08' : status = 'stopped'; msg = 'The detector is not running right now. Please try again later.'; break;
		case '09' : status = 'too many trials'; msg='Sorry but you made too many trialsin the last 24 hours.'; break;
		default : code = '99'; status = 'unknown error'; msg='We are sorry. It seems like we are handling with an unknown error. Please try again later.';
	}

	$('#loader').attr('class','loader1');
	$('#sform').attr('class','sform1');
	$('#id').removeAttr('disabled');
	if (code != '05' && code != '99'){
		add_avatar(id,rand_no);
		ylink1 = '<div class="i" onmouseover="$(this).addClass(\'bgwhite\');" onmouseout="$(this).removeClass(\'bgwhite\');" onclick="javascript:ym_link(2);">send message</div>';
		ylink2 = '<div class="i i2" onmouseover="$(this).addClass(\'bgwhite\');" onmouseout="$(this).removeClass(\'bgwhite\');" onclick="javascript:ym_link(3);">call this id</div>';
		ylink3 = '<div class="i i3" onmouseover="$(this).addClass(\'bgwhite\');" onmouseout="$(this).removeClass(\'bgwhite\');" onclick="javascript:ym_link(4);">view profile</div>';
		ylink4 = '<div class="i i4" onmouseover="$(this).addClass(\'bgwhite\');" onmouseout="$(this).removeClass(\'bgwhite\');" onclick="javascript:ym_link(5);">save avatar</div>';
		//opt2 = '<a rel="nofollow" style="float: right; display: block;" onclick="javascript:check_yform(\''+yid+'\');" href="#">Recheck ID</a>';
	}
	else { fill_input(id); }
	$('#ypanel').slideDown('slow');
	$('#status').html('<span id="status-span" style="color: #'+ col +'">'+status+'</span>');
	$('#msg').html(msg);
	$('#ylinks').html(ylink1 + ylink2 + ylink3 + ylink4);

	//$('#opt').html(opt1 + opt2);

}





so what I can't get

1- the ajax part

2- the method that returns html

what is the meaning that it returns html ?

all the strange methods you will face like 'encode, ...etc) are other parts of the original code
so never mind about them
just tell me 'This line is calling method 'Encode''
and I'll get it

please help me ASAP

I just want the algorithm of these two methods

Is This A Good Question/Topic? 0
  • +

Replies To: I need to know what this code does

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: I need to know what this code does

Posted 24 August 2010 - 11:11 PM

View PostAhmedn1, on 25 August 2010 - 03:53 AM, said:

I have experience with javascript before but don't know about ajax

AJAX is nothing more than making a HTTP request to the server without reloading the page. the server response may either be a text or text and XML. thatís basicly all.
Was This Post Helpful? 0
  • +
  • -

#3 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 23
  • View blog
  • Posts: 557
  • Joined: 04-August 09

Re: I need to know what this code does

Posted 25 August 2010 - 05:51 AM

View PostDormilich, on 24 August 2010 - 10:11 PM, said:

View PostAhmedn1, on 25 August 2010 - 03:53 AM, said:

I have experience with javascript before but don't know about ajax

AJAX is nothing more than making a HTTP request to the server without reloading the page. the server response may either be a text or text and XML. thatís basicly all.


thanks for that
but I need more explanation
can you describe me the algorithm line by line ?
or step by step ?
I'm sorry but I need the algorithm
and the request details and the answer details
to be able to code them in C#

:)
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: I need to know what this code does

Posted 25 August 2010 - 06:37 AM

there are a lot of tutorials explaining that (e.g. at w3schools.com)

most of the time, you donít need to consider the request headers (with the exception of Content-Type, that is different for a POST request), usually, youíre not interested in response headers at all.

on the server side, it is uninteresting as well, you handle it like any other HTTP request. (you only need to take care, if you want to send XML, then you need to send XML headers)

I donít know C#, and I wonít take interest in the near future.
Was This Post Helpful? 0
  • +
  • -

#5 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 23
  • View blog
  • Posts: 557
  • Joined: 04-August 09

Re: I need to know what this code does

Posted 25 August 2010 - 06:51 AM

So can you just tell me what is the request details that are sent ?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: I need to know what this code does

Posted 25 August 2010 - 06:56 AM

for this I can recommend Firefoxí Firebug Add-on, it lets you inspect HTTP requests/responses in its network section.

PS. I donít know how jQuery exactly handles its requests, I can only speak in general.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1