Web Development: jQuery Manipulation

Published on Aug 11, 2009   //  Web Development
Off

Web Development

jQuery includes multiple functions to manipulate the actual HTML of the document you are working with. For instance, you may want to “inject” some text into a specific element on the page. jQuery makes this, and more, very easy.

Today we’ll be going over a couple of the basic manipulation functions, while next week we’ll go over the more complex functions. The first function we’ll look at is text(), using this function, we can replace the current contents of the selected element, with the specified text. If you don’t specify an attribute, jQuery will return the current contents of the selected element to you.

While you can only use strictly text in the text() function, we can use html() to replace the contents with the specified HTML. Like text(), not specifying an attribute will return the current contents.

Let’s have a look at some examples of these:

[javascript]$("div").text(‘Hello world!’);[/javascript]

[javascript]$("div").html(‘<span style="color:red">Hello world!</span>’);[/javascript]

[javascript]if ($("div").text() == ‘Hello world!’) {
$("div").html(‘<strong>Hello world!</strong>’);
}[/javascript]

Web Development: Understanding GET and POST

Published on Jun 30, 2009   //  Web Development

Web Development

Before we can get right into working with Ajax, we need to understand some concepts. When we’re talking about sending and retrieving data from the server, we’re talking about HTTP requests. HTTP requests is an important part of the web, it allows us to exchange data with external or internal hosts.

There are two distinct types of HTTP requests that you need to know: GET and POST. While they may seem similar, they both have their specific uses, and being able to decide which is use is important.

GET requests essentially just request a file from a server. However, it is also possible to pass parameters and values along with the request. If you’ve ever noticed question marks and ampersands in a URL, then you’ve noticed a GET request. GET requests use the URL to pass those parameters and values to the server. This makes GET requests an insecure method of sending the server data. However, since not all data needs to be secure, GET requests are still useful. Take a search query, for example. A search query is not something that needs to be transmitted in a secure way (compared to a password, which should always use a secure method), so a GET request will do perfectly.

POST requests essentially send data to the server. Unlike GET requests, POST requests are secure because they don’t use the URL to transmit the data. POST requests also transmit a URL, just like GET requests. As well, just like GET requests again, it is possible to receive data back from the server with a POST request. Because POST requests send their parameters and values in a secure way, it is more suitable for transmitting passwords and personal details (from a form or otherwise) than GET requests are.

Another difference between GET and POST requests is that GET requests are, by default, cached by browsers, while POST requests are not cached by browsers. This is because POST requests can contain sensitive data, as well submitting the POST data more than once could have negative side effects with poorly programmed websites.

Now that you’re aware of the differences between GET and POST HTTP requests, we’ll be able to begin with the basics of Ajax next week.