AJAX Introduction

12th Jan 2010 at 1:54 PM | Posted in AJAX LEARNING | 1 Comment

AJAX = Asynchronous JavaScript and XML.
AJAX is based on JavaScript and HTTP requests.
AJAX is not a new programming language, but a new way to use existing standards.
With AJAX, a JavaScript can communicate directly with the server, with the XMLHttpRequest object. With this object, a JavaScript can trade data with a web server, without reloading the page.

Internet-applications have many benefits over desktop applications; they can reach a larger audience, they are easier to install and support, and easier to develop.
However, Internet-applications are not always as “rich” and user-friendly as traditional desktop applications.
With AJAX, Internet applications can be made richer and more user-friendly.

AJAX uses the XMLHttpRequest object

To get or send information from/to a database or a file on the server with traditional JavaScript, you will have to make an HTML form, and a user will have to click the “Submit” button to send/get the information, wait for the server to respond, then a new page will load with the results. Because the server returns a new page each time the user submits input, traditional web applications can run slowly and tend to be less user-friendly.

With AJAX, your JavaScript communicates directly with the server, through the JavaScript XMLHttpRequest object.

My 1st ajax script

12th Jan 2010 at 1:50 PM | Posted in AJAX LEARNING | Leave a comment

/************ starts ***************/
1st create a div:
<div id=”test”>
<h2>Click to let AJAX change this text</h2>
</div>
Add two buttons &  add a onclick function.
<button type=”button” onclick=”loadXMLDoc(‘test1.txt’)”>Click Me</button>
<button type=”button” onclick=”loadXMLDoc(‘test2.txt’)”>Click Me</button>

Finally place the script into head section.
<script type=”text/javascript”>
function loadXMLDoc(url)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{document.getElementById(‘test’).innerHTML=xmlhttp.responseText}
}
xmlhttp.open(“GET”,url,false);
xmlhttp.send(null);
document.getElementById(‘test’).innerHTML=xmlhttp.responseText;
}
</script>
Now you need two .txt file.
create “test1.txt” &amp; “test2.txt” &amp; write any thing in those page. Save them in same directory where the script is saved.
/*********** ends *********************/

Ajax responseText

12th Jan 2010 at 1:34 PM | Posted in AJAX LEARNING | Leave a comment

Sending an AJAX Request to a Server(Methodes open()/send()):
To send a request to a web server, we use the open() and send() methods.
The open() method takes three arguments. The first argument defines which method to use (GET or POST). The second argument specifies the name of the server resource (URL). The third argument specifies if the request should be handled asynchronously.
The send() method sends the request off to the server. If we assume that requested a file called “time.asp”, the code would be:

Get the responsetext by xmlhttp.responseText &amp; put this in the div whose id is test.

By changing the third parameter in the open call to “true”, you tell the XMLHttpRequest object to continue the execution after the request to the server has been sent.
Because you cannot simply start using the response from the server request before you are sure the request has been completed, you need to set the onreadystatechange property of the XMLHttpRequest, to a function (or name of a function) to be executed after completion.
In this onreadystatechange function you must test the readyState property before you can use the result of the server call.

State Description
0 The request is not initialized
1 The request has been set up
2 The request has been sent
3 The request is in process
4 The request is complete

AJAX Suggest

12th Jan 2010 at 1:18 PM | Posted in AJAX LEARNING | 2 Comments

http://www.w3schools.com/ajax/ajax_source.asp

jquery live/die

12th Jan 2010 at 11:25 AM | Posted in Jquery | Leave a comment

live:

Binds a handler to an event (like click) for all current – and future – matched element. Can also bind custom events.

<script>
  $(document).ready(function(){
    
    $("p").live("click", function(){
      $(this).after("<p>Another paragraph!</p>");
    });

  });
  </script>
  <style>
  p { background:yellow; font-weight:bold; cursor:pointer; 
      padding:5px; }
  p.over { background: #ccc; }
  span { color:red; }
  </style>

 <p>Click me!</p>
  <span></span>

die:
This does the opposite of live, it removes a bound live event.Without any arguments, all bound live events are removed.

You can also unbind custom events registered with live.

<script>
  $(document).ready(function(){
    
    function aClick() {
      $("div").show().fadeOut("slow");
    }
    $("#bind").click(function () {
      $("#theone").live("click", aClick)
                  .text("Can Click!");
    });
    $("#unbind").click(function () {
      $("#theone").die("click", aClick)
                  .text("Does nothing...");
    });

  });
  </script>
  <style>
  button { margin:5px; }
  button#theone { color:red; background:yellow; }
  </style>

 <button id="theone">Does nothing...</button>
  <button id="bind">Bind Click</button>
  <button id="unbind">Unbind Click</button>
  <div style="display:none;">Click!</div>

jquery Events/trigger

12th Jan 2010 at 11:14 AM | Posted in Jquery | Leave a comment
Trigger an event on every matched element.

This will also cause the default action of the browser with the same name (if one exists) to be executed. For example, passing ‘submit’ to the trigger() function will also cause the browser to submit the form. This default action can be prevented by returning false from one of the functions bound to the event.

<script>
  $(document).ready(function(){

    $("button:first").click(function () {
      update($("span:first"));
    });
    $("button:last").click(function () {
      $("button:first").trigger('click');
      update($("span:last"));
    });
    function update(j) {
      var n = parseInt(j.text(), 10);
      j.text(n + 1);
    }
  });
  </script>
  <style>
  button { margin:10px; }
  div { color:blue; font-weight:bold; }
  span { color:red; }
  </style>
  <button>Button #1</button>
  <button>Button #2</button>
  <div><span>0</span> button #1 clicks.</div>
  <div><span>0</span> button #2 clicks.</div>

jquery Events/bind/unbind

12th Jan 2010 at 9:52 AM | Posted in Jquery | Leave a comment
Binds a handler to one or more events (like click) for each matched element. Can also bind custom events.

Possible event values: blur, focus, load, resize, scroll, unload, beforeunload, click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, mouseenter, mouseleave, change, select, submit, keydown, keypress, keyup, error, ready

<style>
  p { background:yellow; font-weight:bold; cursor:pointer;
      padding:5px; }
  p.over { background: #ccc; }
  span { color:red; }
  </style>

<script>
$(document).ready(function(){

$(“p”).bind(“click”, function(e){

      var str = "( " + e.pageX + ", " + e.pageY + " )";
      $("span").text("Click happened! " + str);
    });
    $("p").bind("dblclick", function(){
      $("span").text("Double-click happened in " + this.tagName);
    });
    $("p").bind("mouseenter mouseleave", function(e){
        $(this).toggleClass("over");
    });
});
</script>
<p>Paragraph</p>
<span></span>

Unbind:

This does the opposite of bind, it removes bound events from each of the matched elements.

Without any arguments, all bound events are removed. If the type is provided, all bound events of that type are removed. If the function that was passed to bind is provided as the second argument, only that specific event handler is removed.
You can also unbind custom events registered with bind.

<style>
button { margin:5px; }
button#theone { color:red; background:yellow; }
</style>
$(document).ready(function(){
function aClick() {
$(“div”).show().fadeOut(“slow”);
}
$(“#bind”).click(function () {
// could use .bind(‘click’, aClick) instead but for variety…
$(“#theone”).click(aClick).text(“Can Click!”);
});
$(“#unbind”).click(function () {
$(“#theone”).unbind(‘click’, aClick).text(“Does nothing…”);
});
});
</script>
<button id=”theone”>Does nothing…</button>
<button id=”bind”>Bind Click</button>
<button id=”unbind”>Unbind Click</button>
<div style=”display:none;”>Click!</div>

jquery addClass & removeClass

12th Jan 2010 at 9:26 AM | Posted in Jquery | Leave a comment

<style>
.red
{
background-color:#CC0033;
color:green;
font-weight:bold;
}
.green
{
background-color:#003300;
color:red;
font-weight:bold;
}
#myDiv
{
border:3px double #000000;
}
</style>
<script>
$(document).ready(function(){
$(“#addclass_red”).click(function ()
{
$(“#myDiv”).addClass(“red”);
$(“#myDiv”).removeClass(“green”);
});
$(“#addclass_green”).click(function ()
{
$(“#myDiv”).addClass(“green”);
$(“#myDiv”).removeClass(“red”);
});
});
</script>
<div id=”myDiv” style=”width:100px; height:30px;”>div 1</div>
<input type=”button” id=”addclass_red” name=”addclass_red” value=”Add Red” />
<input type=”button” id=”addclass_green” name=”addclass_green” value=”Add Green” />
when click on “Add Red” button the function add red class to div myDiv & removing the class green.

similarly, when click on “Add Green” button the function add greenclass to div myDiv & removing the class red.

JQUERY next()

12th Jan 2010 at 9:05 AM | Posted in Jquery | Leave a comment

we can access the div or span… using jquery next() function.

$(document).ready(function(){
$(“#myDiv”).css(“border”,”3px solid red”);
$(“#myDiv”).click(function ()
{
$(this).hide(“slow”);
$(this).show(“slow”, function(){
$(this).css(“border”,”3px solid blue”);
$(this).next(‘div’).next(‘span’).css(“color”,”red”);
});
});
});

<div id=”myDiv” style=”width:100px; height:30px;”>div 1</div>
<div>div 2</div>
<span>span 1</span>

In this example i accessed the span that is next to div2 which is next to div1(div1->div2->span1) by jquery next function.

Example: 2

<script>
$(document).ready(function(){
$(“button[disabled]“).next().text(“this button is disabled”);
});
</script>
<style>
span { color:blue; font-weight:bold; }
button { width:100px; }
</style>
</head>
<body>
<div><button disabled=”disabled”>First</button> – <span></span></div>
<div><button>Second</button> – <span></span></div>
<div><button disabled=”disabled”>Third</button> – <span></span></div>
</body>
</html>

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.

Follow

Get every new post delivered to your Inbox.