/    Sign up×
Community /Pin to ProfileBookmark

Reducing AJAX code and even simple JavaScript

Whoever uses AJAX already felt the need of an easy way to extract data from a XML response send by the server. Probably many already created one.

The usual way to work with the XML response is:
[SIZE=3]document.getElementById(“name”).innerHTML = XmlHttpRequest.responseXML.getElementsByTagName(“name”)[0].childNodes[0].nodeValue;
[/SIZE]

It is not really long but think how big will be your script after ten, twenty, thirty lines like this. Some people to reduce the code make like this:
[SIZE=3]var xmlresponse = XmlHttpRequest.responseXML;
document.getElementById(“name”).innerHTML = xmlresponse.getElementsByTagName(“name”)[0].childNodes[0].nodeValue;[/SIZE]

But this isn’t really much short so this is my way to reduce it. First a very little function.
[SIZE=3]function getXMLData(objXmlHttp,tag) {
return objXmlHttp.responseXML.getElementsByTagName(tag)[0].childNodes[0].nodeValue;
}[/SIZE]

new code now will look like this:
[SIZE=3]document.getElementById(“name”).innerHTML = getXMLData(XmlHttpRequest,”name”);[/SIZE]

for a code even shorter why can’t we reduce the way we get the field?
[SIZE=3]function getField(id) {
return document.getElementById(id);
}[/SIZE]

Our new code will be like this.
[SIZE=3]getField(“name”).innerHTML = getXMLData(XmlHttpRequest,”name”);[/SIZE]

Hope some of you like this. If you post somewhere else give at least the credits. Got this simple(silly) idea working on my thesis about AJAX.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Dec 10.2006 — I am guessing you hanot woked with any of the major libraries like dojo, jquery,or prototype. $() is a great example.

Eric
Copy linkTweet thisAlerts:
@cyberowlauthorDec 10.2006 — Frameworks are nice if you want visual effects that take too much code.

I like to build and take care of my own scripts so I can adapt them for each situation. Hate this habit that people has of copying without knowing how the code works.

That's my opinion. No obligation to agree. ? (I read your book, nice)
Copy linkTweet thisAlerts:
@cyberowlauthorDec 11.2006 — Have been looking jquery framework.

I agree that some features are really interesting, but don't you agree too that simple XMLHttpRequests aren't that much hard to code?

To use these kind of frameworks that are so extended you need to read tutorials and tutorials to get to understand what each function does and how and where you can use it.

My code don't intend to become a framework, just a content reducer for the ones who like to code javascript themselves.

I created these cause while I was doing my application as example for my thesis about AJAX, I started to get to many lines that did the same, entering information in input fields or page elements. Think about the mess...

I liked very much the idea of using the $ sign, but still think that it is important to add some word so you can understand the purpose of the function.

[CODE]
function $data(objXmlHttp,tag) {
return objXmlHttp.responseXML.getElementsByTagName(tag)[0].childNodes[0].nodeValue;
}

function $field(id) {
return document.getElementById(id);
}

$field("name").innerHTML = $data(XmlHttpRequest,"name");
[/CODE]


PS: I'm changing my old code to use the $functions and realized that I'm using it more than a 100 times, I an application with only four forms.
Copy linkTweet thisAlerts:
@A1ien51Dec 11.2006 — 
I agree that some features are really interesting, but don't you agree too that simple XMLHttpRequests aren't that much hard to code?
[/QUOTE]

It is not the XHR object that is hard, what is hard about it is making sure you have code that is bug free and that it will work in a lot of different enviornments that you have no control over.

To use these kind of frameworks that are so extended you need to read tutorials and tutorials to get to understand what each function does and how and where you can use it.
[/QUOTE]

This is just like any other API that you will run into. I can not just pick up the PDF generator API that my company uses without reading the documentation. I can not pick up another language without reading it. Libraries and Frameworks are big, but they extend JavaScript so you do not have to waste your time doing it!

My code don't intend to become a framework, just a content reducer for the ones who like to code javascript themselves.

I created these cause while I was doing my application as example for my thesis about AJAX, I started to get to many lines that did the same, entering information in input fields or page elements. Think about the mess...
[/QUOTE]

I know I have a section in my code called Helper functions that reduce all of my calls to single lines.


I liked very much the idea of using the $ sign, but still think that it is important to add some word so you can understand the purpose of the function.
[/QUOTE]

Well if you know the library, everyone knows what the shortcuts mean. It is not meant to be discriptive, but turn 23 characters into 1. Saves your fingers!

Eric
Copy linkTweet thisAlerts:
@cyberowlauthorDec 11.2006 — 
This is just like any other API that you will run into. I can not just pick up the PDF generator API that my company uses without reading the documentation. I can not pick up another language without reading it. Libraries and Frameworks are big, but they extend JavaScript so you do not have to waste your time doing it![/QUOTE]

You got that right of course. That's my point, I didn't use any cause I didn't need anything really special for my code. Learn them if you need an special complex functionality, don't you agree?

It is like learning several international languages. I already speak four and my head get some mess with them sometimes, lol ? In the computing area I program Java, JSP, C#, ASP.NET, etc. Have a nice knowledge of CSS and have beeing improving the Javascript knowledge mainly for the use of AJAX. ?

I know I have a section in my code called Helper functions that reduce all of my calls to single lines.[/QUOTE]

That's exactly the only purpose of this code: Helper functions, nothing else. Let's not get crazy about the subject. Just thought that the basic idea could be useful for someone else cause I never saw an example this way in the web.

Well if you know the library, everyone knows what the shortcuts mean. It is not meant to be discriptive, but turn 23 characters into 1. Saves your fingers!
[/QUOTE]

I like descriptive method because of a simple detail, memory. Remembering which function does what is not always simple. 1 character is good but 5-6 will really make your fingers fall? Just kidding. ?
×

Success!

Help @cyberowl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...