Announcement

Collapse
No announcement yet.

Tutorial: AJAX

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Tutorial: AJAX

    AJAX is a javascript based technology, that lets you fetch pages, and use their data in your hosting page, without re-loading them.

    In fact, its very much like server side includes, with respect to allowing external content to be included in your pages, but with the added bonus of allowing the included content to be changed when you want it to, and not when the server puts the page together on the server.

    So, this is all a little complex, but what does it mean for actinic?

    Firstly, it does not require any server side scripting, like php, or asp to make it work. its all client side javascript.

    This is also not very good for SEO, since most search engines have no idea what javascript included content looks like and wont see it in your final pages.

    For example, you could take the information from the popup, normally associated with a product, and add it inline to the page, when the user clicks a button. And then hide it again, when they click it again. In this example, i'm going to simply include some text.

    Here is a javascript function, that we need to trigger an AJAX call.

    Code:
    function ajaxGetPage(pageUrl,divID)
      {
    var d = new Date();
    var time = d.getTime();
    pageUrl=pageUrl+'&t='+time;
    
      var xmlHttp;
      try
        {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
        }
      catch (e)
        {
        // Internet Explorer
        try
          {
          xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
          }
        catch (e)
          {
          try
            {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
          catch (e)
            {
            alert("Your browser does not support AJAX!");
            return false;
            }
          }
        }
        xmlHttp.onreadystatechange=function()
          {
          if(xmlHttp.readyState==4)
            {
    		//alert (xmlHttp.responseText);
    
    		  if(divID=="dispcontent") {
    		  document.getElementById(divID).innerHTML = xmlHttp.responseText;
    		  }
    		  else {
    			if(document.getElementById(divID).innerHTML=="") {
    				document.getElementById(divID).innerHTML = xmlHttp.responseText;
    				}
    			else {
    			document.getElementById(divID).innerHTML = "";
    			}
    		  }
            }
          }
        xmlHttp.open("GET",pageUrl,true);
        xmlHttp.send(null);
    
      }
    looks complex, doesnt it?

    Its actually more complex than it should be, but only becasue i'v added some clever browser compatibility i found floating around on the web.

    notice this part, right at the top of the code: ajaxGetPage(pageUrl,divID)

    'ajaxGetPage' is the name of the function. We'll use this later on, to call this block of code.
    'pageurl' is the name of the page we want to load date from. This is going to be simple text in this example.
    'divID' is the name of the container, that we will insert the code from our fetched page into.

    It works like this:
    1: the user clicks the link triggering the function above
    2: the function uses AJAX to fetch the page we want to fetch.
    3: the function inserts the data into the host page without re-loading.
    4: the loaded content assumes any styles and colours that the parent page has.
    5: the user clicks on the link again, and the data in the container is removed.

    Next we need a container to keep our data in once we load it.

    HTML Code:
    <div id="mytarget"></div>
    Remember earlier, the 'divID' variable in our function? if the name 'mytarget' is passed into it, then, it will tell our function to load the data into that container. easy huh?

    Now, we need to make a link. This link will have some javascript, that causes the function to trigger.

    In this simple example, mine looks like this:

    HTML Code:
    <a href="#" onclick="ajaxGetPage('include.htm','mytarget')">click here to toggle</a>
    but wait? whats this 'include.htm' you can see in the link? well, thats the data we'll be including. it can be anything you want. it could just as easily be one of your info_000.html files that actinic generates.

    Remember, that the included page will be included in your sites html structure. so remember to remove all the body, html and head tagging. sicne the parent page allready has those!

    Included is a very simple working example. 2 files. 'parent.html' and 'child.html'. put these in the same folder and load 'parent.html' to see ajax in action.

    REMEMBER: THIS IS A SERVER TECHNOLOGY ONLY. it wont work on your desktop. you'll need to upload it to an actual webserver.

    hope this has been any help to anyone learning AJAX.
    Attached Files

    #2
    and I thought Ajax was a cleaning fluid


    now *that* I'd understand!
    Tracey

    Comment


      #3
      *someone* had to say it.

      Comment


        #4
        Gabe

        Wow....

        have you got an example of where you've used this on an actinic site?

        Comment


          #5
          Originally posted by gabrielcrowe
          *someone* had to say it.
          yup
          and this dim one took the bait
          Tracey

          Comment


            #6
            Originally posted by pinbrook
            Gabe

            Wow....

            have you got an example of where you've used this on an actinic site?
            well, its very easy to make it work with V8

            drop the code into your templates, but instead of calling the 'child.html' call the info_000.html file of the popup page.


            remember to edit the template of the popup so that no head body or html tags exist.

            they will break your outer template, ending the html too soon.

            i dont know the variable to do it but:

            <a href="#" onclick="ajaxGetPage('THIS_IS_THE_URL_OF_THE_POPUP','mytarget')">click here to toggle</a>

            oh and i almost forgot... i'v never used this on an actinic store. its just one of the many emerging web technologies that are taking over.

            http://script.aculo.us/ for example, and the prototype framework.

            this type of code features heavily in our Actinet Product. its important to keep the accesses to the database to nibbles, instead of big honking bites. speed is of the issue, and AJAx allows us to break a large page of multiple database acceses, down to several smaller ones, increasing overall page load responsiveness.

            see, the AJAX is executed *after* the page renders, so that your requested data is loaded in the background.

            Comment


              #7
              Excellent AJAX scripting there Gabriel.

              You can see it in Actinic working here:

              http://www.teclan.com/AJAX/acatalog/Books.html
              [Note: Its only on the first product and I left a normal ext info lin above the AJAX one for comparison]

              A very simply example, but it shows the difference between the normal ext info link and the AJAX version. Very smooth.
              There are many exciting new UI based effects and tricks that can be achieved with AJAX.

              Note:
              I had to tweak the AJAX scripting a little to get it working. I had to change
              Code:
              function ajaxGetPage(pageUrl,divID)
              //  {
              var d = new Date();
              var time = d.getTime();
              pageUrl=pageUrl+'&t='+time;
              to

              Code:
              function ajaxGetPage(pageUrl,divID)
                {
              var d = new Date();
              var time = d.getTime();
              //pageUrl=pageUrl+'&t='+time;
              i.e. remove the & from the querystring on the request. Not dug too deeply to check why though

              Very interesting though.... and well worth more experimenting. I have already seen a few sites making excellent use of AJAX in Actinic.
              Last edited by fergusw; 23-May-2007, 03:22 PM. Reason: Better instruction
              Fergus Weir - teclan ltd
              Ecommerce Digital Marketing

              SellerDeck Responsive Web Design

              SellerDeck Hosting
              SellerDeck Digital Marketing

              Comment


                #8
                beautiful example there.

                thanks for showing people the actinic integration.

                the reason 'time' is added to thie querystring is actually important.

                IE6 and IE7 use some agressive cache, that caches all the ajax. Essentially, if you update the inserted code, IE fails to use the new version, favouring the cache version every time.

                adding the time causes the page it calles to be 'different'.

                but if the example still works without it, then thats good

                use this:
                HTML Code:
                function ajaxGetPage(pageUrl,divID)
                  {
                var d = new Date();
                var time = d.getTime();
                pageUrl=pageUrl+'?t='+time;
                usually, this function adds time on to the string as the last item, assuming you've allready added some query strings uwing the normal syntax:

                page.htm?item1=this&iitem2=that&t=[time]

                swapping the ampersand for a questionmarks, works fine.

                Comment


                  #9
                  You can see it in Actinic working here:

                  http://www.teclan.com/AJAX/acatalog/Books.html
                  [Note: Its only on the first product and I left a normal ext info lin above the AJAX one for comparison]
                  aaaahhhh Neat!

                  Comment


                    #10
                    I think I have a use for this - please could one of you experts start a thread giving step by step instructions?

                    Gabriel invented it, Fergus applied it & Jo is impressed - now can the rank & file put it into use?
                    Paul
                    Flower-Stands.co.uk - the UK's largest online supplier of Fresh Flower Merchandising Stands

                    Using V10.2 with Norman's brilliantly simple TABBER.

                    Comment


                      #11
                      please could one of you experts start a thread giving step by step instructions?
                      Hmmm ..... here goes. This is how I implemented it. (for V8)

                      Step 1:
                      [Insert the Ajax script into Actinic]
                      Open actiniccore.js (found in site1 directory) in notepad and add the following to the very end
                      Code:
                      /***********************************************************
                      *
                      * AJAX Script
                      *
                      ************************************************************/
                      function ajaxGetPage(pageUrl,divID)
                        {
                      var d = new Date();
                      var time = d.getTime();
                      pageUrl=pageUrl+'?t='+time;
                      
                        var xmlHttp;
                        try
                          {
                          // Firefox, Opera 8.0+, Safari
                          xmlHttp=new XMLHttpRequest();
                          }
                        catch (e)
                          {
                          // Internet Explorer
                          try
                            {
                            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
                            }
                          catch (e)
                            {
                            try
                              {
                              xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                              }
                            catch (e)
                              {
                              alert("Your browser does not support AJAX!");
                              return false;
                              }
                            }
                          }
                          xmlHttp.onreadystatechange=function()
                            {
                            if(xmlHttp.readyState==4)
                              {
                              //alert (xmlHttp.responseText);
                      
                                if(divID=="dispcontent") {
                                document.getElementById(divID).innerHTML = xmlHttp.responseText;
                                }
                                else {
                                  if(document.getElementById(divID).innerHTML=="") {
                                      document.getElementById(divID).innerHTML = xmlHttp.responseText;
                                      }
                                  else {
                                  document.getElementById(divID).innerHTML = "";
                                  }
                                }
                              }
                            }
                          xmlHttp.open("GET",pageUrl,true);
                          xmlHttp.send(null);
                      
                        }
                      Step 2: [amend the Extended info layout]
                      First I copied an existing Extended info layout from the library from "design | Library" and renamed it something like "AjaxExtendedInfo". I then amended the HTML code in the layout to remove eveything except

                      Code:
                      <Actinic:Variable Name="ExtendedInfoText"/>
                      Step 3: [Choose the ext info layout]
                      I made sure that I selected the newly created extended info layout for the product I wanted this to work for. Making sure, also, that it has extended info page ticked etc....

                      Step 4: [Adding the <div> on product layout where you want the ext info text to appear]
                      I amended a product layout and added

                      Code:
                      <div id="mytarget"></div>
                      to the layout where I wanted the additional ext info text to appear. In my example I placed it just before the "ProductAlsoBought" layout.

                      Step 5: [Adding the AJAX function]
                      Finally I amended the product layout (via the design tab again) to change

                      Code:
                      <a href="javascript:ShowPopUp('<actinic:variable name="ExtendedInfoPageEncoded" />',<actinic:variable name="ExtInfoWindowWidth" />,<actinic:variable name="ExtInfoWindowHeight" />);">
                               <actinic:variable name="ExtendedInfoLinkText" />
                            </a>
                      to
                      Code:
                      <a href="#" onclick="ajaxGetPage('<actinic:variable name="CatalogURL" /><actinic:variable name="ExtendedInfoPageName" />','mytarget')"><actinic:variable name="ExtendedInfoLinkText" /></a>
                      I then made sure that I had Display by "text link" for the extended info page selected.
                      I uploaded and, hey presto!

                      It is not 100% fully tested, however this should be enough to get you under way.
                      Fergus Weir - teclan ltd
                      Ecommerce Digital Marketing

                      SellerDeck Responsive Web Design

                      SellerDeck Hosting
                      SellerDeck Digital Marketing

                      Comment


                        #12
                        oop! you forgot to add the love

                        *sprinkles love on the whole kit and kaboodle*

                        Comment


                          #13
                          Excellent stuff.

                          We use a little bit of a AJAX currently in v8 to handle the advanced best sellers and new products lists (i.e. the lists are generated as separate pages and then included in the site pages on the server). This is in the Advanced User Guide.

                          Comment


                            #14
                            is the ajax in actinic re-usable? to save writing code that does not need to be written?

                            Comment


                              #15
                              Chris,
                              I take it you are referring to the

                              Code:
                              function HtmlInclude()
                              in the actiniccore.js file?

                              Could this not be adapted to accept parameters and allow this usage as well?

                              FYI : I've been playing a little with different extended info layouts on this example

                              http://www.teclan.com/AJAX/acatalog/Books.html


                              With a little styling and a tweak to the AJAX java code (to hide/unhide the DIV element) you can control individual layouts for each product.
                              Fergus Weir - teclan ltd
                              Ecommerce Digital Marketing

                              SellerDeck Responsive Web Design

                              SellerDeck Hosting
                              SellerDeck Digital Marketing

                              Comment

                              Working...
                              X