Posts Tagged ‘javascript’

I spent a large part of last weekend coding some changes to CommentLuv, well, changing the way it can be called and how it displays posts and such. Here’s a little preview of what sort of thing it can do on a dummy form, let me know if it can’t pick up your last post or if you experience any issues with it…


This is running as just javascript and jquery without using any of the wordpress core functions and it should work on standard forms on sites that don’t have PHP enabled. I just need to tidy up the code and package it in an easy to install way and it’ll open up CommentLuv to many more platforms…yey!

I’m not 100% sure yet but this could mean a blogspot commentluv plugin, I’m trying some experiments now and so far the results are promising…

Popularity: 5% [?]

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

I have had to change the display code for the aqua game because it seems that some blogs try and reformat the characters within the javascript and that messes with the layout, I tried to do it like this because I needed to make sure that the referring page was included in the variables but I think I have found a much better way to do it automatically within the code on my server instead.

If you want to display the game on your site, just use this single line of code (you may have to do it in html source mode) and it should work properly...

JAVASCRIPT:
  1. <SCRIPT language="JavaScript" SRC="http://www.imgames.co.uk/games/aqua/remoteaqua.js"></SCRIPT>

Please bear in mind that if you want to be qualified to win when one of your readers do, you need to register your site address for the game and your email address so I can contact you if you win..., you can do that here...

Email address*

Your Name*

Phone

your site*

Or you can just put the code on it's own to allow your readers to play the aqua game without the chance of you winning too. More games are coming soon, some are full of action!!

Popularity: 3% [?]

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

AJAX Avenue

This is part 5 of the AJAX Tutorial Series.
You can see the other Beginners AJAX Tutorials here.

Hurrah! this tutorial will actually download some data (yey!) We will use everything we have learned so far and add just a little bit that will allow us to download data from the server and put it somewhere on the page without a refresh. (I know, you must have goosebumps!) :-)

XMLHttpRequestObject.send(null);

We need this command to initiate the object send. We created an object, created a place for the data to go, we created a routine to wait patiently for the object to be ready and sent the object to the data source using the above command. The patient routine for the waiting will take the response to the data source and put it in the div we created in the HTML of the page.

We can replace the bit from the last bit of complete code (on part 4) with this command and instead of it saying it's ready to download, it will actually do the fetching for the obj.innerHTML=XMLHttpRequestObject.responseText; which is called when the readyState and Status are ok.

HTML:
  1.     <title>FiddyP AJAX Tutorial Series Part 5</title>
  2.     <script language = "javascript">
  3.       var XMLHttpRequestObject = false;
  4.       if (window.XMLHttpRequest) {
  5.         XMLHttpRequestObject = new XMLHttpRequest();
  6.       } else if (window.ActiveXObject) {
  7.         XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  8.       }
  9.       function getData(dataSource, divID)
  10.       {
  11.         if(XMLHttpRequestObject) {
  12.           var obj = document.getElementById(divID);
  13.           XMLHttpRequestObject.open("GET", dataSource);
  14.           XMLHttpRequestObject.onreadystatechange = function()
  15.           {
  16.             if (XMLHttpRequestObject.readyState == 4 &&
  17.               XMLHttpRequestObject.status == 200) {
  18.                 obj.innerHTML=XMLHttpRequestObject.responseText;
  19.             }
  20.           }
  21.        XMLHttpRequestObject.send(null);
  22.         }
  23.       }
  24.     </script>
  25.   </head>
  26.     <H1>Fetching data with Ajax</H1>
  27.     <form>
  28.       <input type = "button" value = "Display Message"
  29.         onclick = "getData('oranges.txt', 'targetDiv')">
  30.     </form>
  31.     <div id="targetDiv">
  32.       <p>The fetched data will go here.</p>
  33.     </div>
  34.   </body>
  35. </html>

I'll try and make this code easier to understand with a picture using pretty colours! :-)
Part 5 AJAX picture

The blue bit gets called when the user clicks the button, if the object was successfully created then create a routine that 'listens' to the objects readyState and status. Then the XMLHttpRequestObject.send(null); command does the action and when that goes ahead and activates XMLHttpRequestObject.open("GET", dataSource); which goes off and gets the data pointed to by dataSource which of course changes the objects status and readyState. When it is at what we want, we inject the responseText to the div pointed to by divID.

Simple! :-/

You can see it in action here

Exciting huh? LOL . It'll display anything that is in the oranges.txt file, not much use yet but at least we know we can fetch data!

What if, instead of displaying the contents of a text file, we used a php script to 'echo' out something?.. replace oranges.txt with this code..

PHP:
  1. echo "these are some oranges created by php!";
  2. echo "<br/>ooooooooooooooooooooooooooooooooo";
  3. ?>

(don't forget to enclose any php script with <?php and ?> or it wont work - I cant put them in here otherwise it will run the script instead of showing the code. Also, change the onclick event in the html to call oranges.php (if you saved the php with that name) instead of oranges.txt

You can see that in action here

There may be a pause between you clicking and the message being displayed. We can let the user know that something is happening by adding another if condition to look for a readyState of 1 (loading) and displaying the message "off to get oranges..." like this..

HTML:
  1. if (XMLHttpRequestObject.readyState == 1)
  2. {
  3.     obj.innerHTML="off to get oranges...";
  4. }

we just chuck that into the anonymous function that is called when the readyState changes...

HTML:
  1. XMLHttpRequestObject.onreadystatechange = function()
  2.           {
  3.             if (XMLHttpRequestObject.readyState == 4 &&
  4.               XMLHttpRequestObject.status == 200) {
  5.         obj.innerHTML=XMLHttpRequestObject.responseText;
  6.  
  7.             }
  8.         if (XMLHttpRequestObject.readyState == 1)
  9.         {
  10.         obj.innerHTML="off to get oranges...";
  11.         }
  12.           }

See that in action here

You can download all the files used in this tutorial here.

Now we're getting somewhere! But, what if we want to send data to the php file so it can do something more than print a hardcoded message?
Actually, it's quite easy! come back next week to see the final part where we'll send something to the php file and have it send back something it made with what we sent. (it could be a username or contents of a text field - think form validation!)

You can subscribe to my feed with the button at the top right of this blog or add your email to the box on the sidebar of the main page to subscribe by email. I hope you can come back and learn a bit more, enough at least to make your own php script and AJAX page....

Popularity: 10% [?]

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Ajax Avenue Street Sign

This is part 4 of the AJAX Tutorial Series.
You can see the other Beginners AJAX Tutorials here.

In this part of the tutorial series we will get ready to rumble! download data using the object we created in the last part...

onreadystatechange

What the hell is that? lol, it's not move states when you're ready.. it's what we use on the object we created to see what state it is in, the different states are:

  • 0 - Uninitialized
  • 1 - Loading
  • 2 - Loaded
  • 3 - Interactive
  • 4 - Complete

We can check for an objects state to see where it's at to decide if we can output the result or not, we can combine this with a test for the HTTP status of a download to see if it was successful, we can check for a value of 200 which means everything went ok. (a value of 404 would mean file not found just like normal HTTP status codes).

To use the readystate property, just bang it on the end of the object name and put that in an 'if' statement. Ie...

if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
{do something}

(the && means AND, so we check the readystate AND the status, if they are BOTH correct then execute the code in the curly brackets)

To put that in the code of the last tutorial, replace the bit that outputs the message about the oranges box being collected with an anonymous function that is attached to the objects readystate. It's not named an anonymous function because it has a secret identity, it is just a nice way of running a function with no name when a certain thing happens like the objects' readystate being changed.

For example, if we put
XMLHttpReqestObject.onreadystatechange = function()
{
do something
}

We have attached the onreadystatechange to the object we created, as soon as that objects readystate has altered, the anonymous function takes action and executes the commands contained in the curly brackets ({do something }).

That's exactly where we put the test to see what the HTTP status and readystate number is. Ie.

XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
{
data downloaded, do something with it...
}
}

You may have noticed little graphics that are synonymous with web 2.0 and AJAX ( ajax loading ). You can add one of those to let the user see something is happening. In the code above, you can check for a readyState of 1 (loading) and add some text like this:

XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 1)
{
obj.innerHtml="Loading...";
}

if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
{
data downloaded, do something with it...
}
}

The FiddyP amazing analogy:
Last time we had created a box for the oranges that we want from the AJAX shop
XMLHttpRequestObject = new XMLHttpRequest();
then we opened it and sent it to the AJAX shop with a label of what we want.
XMLHttpRequestObject.open("GET", dataSource);
And we wait for the signal that the box we sent was filled ok and ready to go
if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)

Your whole code will now look like this...

HTML:
  1.   <head>
  2.     <title>FiddyP AJAX Tutorial Series Part 4</title>
  3.     <script language = "javascript">
  4.       var XMLHttpRequestObject = false;
  5.       if (window.XMLHttpRequest) {
  6.         XMLHttpRequestObject = new XMLHttpRequest();
  7.       } else if (window.ActiveXObject) {
  8.         XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  9.       }
  10.       function getData(dataSource, divID)
  11.       {
  12.         if(XMLHttpRequestObject) {
  13.           var obj = document.getElementById(divID);
  14.           XMLHttpRequestObject.open("GET", dataSource);
  15.  
  16.           XMLHttpRequestObject.onreadystatechange = function()
  17.           {
  18.             if (XMLHttpRequestObject.readyState == 4 &&
  19.               XMLHttpRequestObject.status == 200) {
  20.             }
  21.           }
  22.        obj.innerHTML = "Ready to download";
  23.            
  24.         }
  25.       }
  26.     </script>
  27.   </head>
  28.     <H1>Fetching data with Ajax</H1>
  29.     <form>
  30.       <input type = "button" value = "Display Message"
  31.         onclick = "getData('oranges.txt', 'targetDiv')">
  32.     </form>
  33.     <div id="targetDiv">
  34.       <p>The fetched data will go here.</p>
  35.     </div>
  36.   </body>
  37. </html>

OK, so you have your box at the AJAX shop full of oranges.txt, next tutorial we'll do something with the oranges...

If you've been following the other tutorials then the code above shouldn't be so hard to understand (shyeah right!), it's just a bit more added to what we've already covered. You can copy the code from above and try it out yourself or you can click here to see it in action (hold on to your hat :-P)

We're still in the AJAX starting blocks waiting for the bang to start us on the AJAX runway but don't despair, next tutorial we will actually download data from a server and display it on the page without a reload which is what AJAX is all about. Once we can actually display the data, we can start doing things with it and then we can start to make pretty little scripts that do amazing things like count the amount of times a pink box has been clicked. hehehe, we'll do more than that I promise!

Popularity: 8% [?]

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Ajax Avenue Street Sign

This is part 3 of the AJAX Tutorial Series.
You can see the other Beginners AJAX Tutorials here.

In this part of the tutorial series we will open the object that you created in the last tutorial. So far, we have created an object to handle the data and made sure it is created in whatever browser you are using. Now, if we want to work with the created object, we need to open it...

Open The XMLHttpRequest Object

Opening the object prepares it for use to fetch the data from a server, the act of opening the object doesn't actually fetch any data yet.

The FiddyP amazing analogy :- Imagine you created a page to collect a box of oranges from the local AJAX fruit shop. In the last tutorial, you created the box (object), now you need to open it so the AJAX oranges can fit inside...

The open comand: [square brackets] = optional arguments.

open("method", "url" [, asyncFlag[, "username"[, "password"]]])

Explanation:

  • method is the HTTP method to use when opening the connection. It can be 'GET', 'POST', 'PUT' or 'HEAD'
  • url is the URL you want to download from
  • asyncFlag is a Boolean (one or the other) value used to indicate that the call is asynchronous. (no waiting for it to finish)
    This defaults to TRUE or 1 so is not needed unless you want to specify that the page waits for completion.
  • username is if you need a username to access the page identified by URL
  • password is for a password if it's required.

Make sense? lol, maybe not. Here's how it looks in use:

XMLHttpRequestObject.open("GET","oranges.txt");

We just add the open bit of code on to the end of the name of the object we created. Here's an example page where we create a form in HTML and add a button for the user to click to open a remote file on the server with AJAX. Because AJAX uses CSS to change elements of the page, we need to create a <div> on the page to show the data we retrieve from the remote file. We'll also have to create a function to take action when the button is clicked (we can't do it the normal way with the <form> action because that would cause the page to reload).

Here's the form and div html:

HTML:
  1.   <input type = "button" value = "Display Message" onclick = "getData('oranges.txt', 'targetDiv')">
  2. </form>
  3.  
  4. <div id="targetDiv">
  5.   <p>The oranges box will go here.</p>
  6. </div>

You can see that the button has an 'onclick' event. That's the javascript bit that calls the function when the button is clicked. The <div> has an id so the function will know where to put the collected data.

The javascript function is held in the <head> part of the document;

JAVASCRIPT:
  1. function getData(dataSource, divID)
  2.       {
  3.         if(XMLHttpRequestObject) {
  4.           var obj = document.getElementById(divID);
  5.           XMLHttpRequestObject.open("GET", dataSource);
  6.  
  7.           obj.innerHTML="Oranges Box Opened!";
  8.         }
  9.       }

The function is called with the name of the data file we want (dataSource) and where to put the output (divID)
The first line checks if the XMLHttpRequestObject was created (we did that last time so you know it should get to this point)
If it finds that the object was created, it moves on to set the variable obj to the <div> we created by using getElementByID
Next, we use the open command on the object we created earlier, set the option to GET and point it to the external data file we specified when calling the function.
Once that is done, we can output a message to the <div> we assigned to the variable obj by using .innerHTML=

We'll just put this function in place of the "hello world" message that we put in the last tutorial..

The whole code looks like this...

HTML:
  1.   <head>
  2.     <title>FiddyP AJAX tutorial, open the object</title>
  3.     <script language = "javascript">
  4.       var XMLHttpRequestObject = false;
  5.       if (window.XMLHttpRequest) {
  6.         XMLHttpRequestObject = new XMLHttpRequest();
  7.       } else if (window.ActiveXObject) {
  8.         XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  9.       }
  10.  
  11.       function getData(dataSource, divID)
  12.       {
  13.         if(XMLHttpRequestObject) {
  14.           var obj = document.getElementById(divID);
  15.           XMLHttpRequestObject.open("GET", dataSource);
  16.           obj.innerHTML="Oranges Box Opened!";
  17.         }
  18.       }
  19.     </script>
  20.   </head>
  21.  
  22.     <H1>Fetching data with Ajax</H1>
  23.     <form>
  24.       <input type = "button" value = "Display Message"
  25.         onclick = "getData('oranges.txt', 'targetDiv')">
  26.     </form>
  27.     <div id="targetDiv">
  28.       <p>The Oranges box will go here.</p>
  29.     </div>
  30.   </body>
  31. </html>

You can copy the contents of the HTML shown above and save it as a HTML file, you can also create a file called oranges.txt and put it in the same directory as the HTML file. (it doesn't matter what is in the text file as we wont be doing anything with it other than to open it). Open the HTML file in your browser and try it yourself or click here to see it in action if you can't wait.

That's it for this week, we're still not doing anything major yet but, be happy! we have so far, created an object and opened it. YEY! come back for part 4 where we will prepare to download information from the file we opened. (oh, I bet you can't wait!!)

beep beep w00t!

Popularity: 6% [?]

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]