+ Reply to Thread
Page 1 of 3
1 2 3 LastLast
Results 1 to 10 of 27

Thread: Easy, accessible, AJAX.

  1. #1
    likes chips. Scriptage's Avatar
    Join Date
    May 2008
    Location
    Yorkshire
    Posts
    2,757

    Easy, accessible, AJAX.

    There have been quite a few people wanting to replace content within a frame on a website without navigating away from the page so I've written a lightweight framework for doing just that for anybody to use.

    First you need to create a template design, there are three major components that need to be included; two Javascripts (ajax.js and links.js) and a div with an id of 'content' (this is where all of our content will be displayed).

    The first Javascript (ajax.js) needs to be placed in the head of the document whilst the second is placed just above the end body tag. The content div can be placed anywhere you like:

    PHP Code:
    <!DOCTYPE html 
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    >
    <
    html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <
    head>
    <
    title>AJAX Example</title>
    <
    link rel="stylesheet" href="styles/default.css" />
    <
    script type="text/javascript" src="javascript/ajax.js"></script>
    </head>
    <body>
    <ul id="navigation">
    <li><a href="index.html" class="ajax">Home</a></li>
    <li><a href="one.html" class="ajax">One</a></li>
    </ul>
    <div id="content">
    Home
    </div>
    <script type="text/javascript" src="javascript/links.js"></script>
    </body>
    </html> 
    As you can see from the code above we have placed a class of ajax in the link tag of the link that we wish to retrieve using ajax. When one of these links is clicked the Javascript retrieves the target document (href) and attempts to copy the contents of the 'content' div, if no content div is found or the user does not have Javascript enabled the href is opened in the window instead allowing for a graceful transformation.

    Remeber for a consistent design the only part of the page that should change should be the content in the content div of each page.

    ajax.js (compressed):

    PHP Code:
    function GetRequestObject(){var a;/*@cc_on@if(@_jscript_version>=5)try{a=new ActiveXObject("Msxml2.XMLHTTP")}catch(e){try{a=new ActiveXObject("Microsoft.XMLHTTP")}catch(e){a=false}}@else a=false;@end@*/if(!a&&typeof XMLHttpRequest!='undefined'){try{a=new XMLHttpRequest()}catch(e){a=false}}return a}function XMLDOMAdapter(){if(typeof ActiveXObject!='undefined'){return new(function(){this.createDocument=function(){var a=['Microsoft.XMLDOM','Msxml2.DOMDocument.3.0','MSXML2.DOMDocument','MSXML.DOMDocument'];for(var b in a){try{return new ActiveXObject(a[b])}catch(e){}}return false};this.serialize=function(a){return a.xml};this.parseXml=function(a){var b=this.createDocument();if(!b.loadXML(a)){return false}return b}})()}else if(document&&document.implementation&&document.implementation.createDocument&&typeof DOMParser!='undefined'){return new(function(){this.createDocument=function(){return document.implementation.createDocument('','',null)};this.serialize=function(a){return new XMLSerializer().serializeToString(a)};this.parseXml=function(a){var b=new DOMParser().parseFromString(a,'text/xml');if(b.documentElement.nodeName=='parsererror'){return false}return b}})()}else{return false}};function DeepCopyNode(a,b){var c;if(!b){c=document.createElement(a.nodeName)}else{c=b.appendChild(document.createElement(a.nodeName))}for(var j=0;j<a.attributes.length;j++){c.setAttribute(a.attributes[j].nodeName,a.attributes[j].nodeValue)}if(c.getAttribute('class')=='ajax'){c.onclick=function(){SendRequest(c.attributes.getNamedItem('href').value);return false}}for(var i=0;i<a.childNodes.length;i++){if(a.childNodes[i].nodeType==1){DeepCopyNode(a.childNodes[i],c)}else if(a.childNodes[i].nodeType==3){var d=document.createTextNode(a.childNodes[i].nodeValue);c.appendChild(d)}}return c}function SendRequest(f){request=GetRequestObject();if(!request){window.location.href=f}request.open("GET",f,true);request.onreadystatechange=function(){if(request.readystate==4||request.readyState==4){var a=request.responseText.substring(request.responseText.indexOf('<html'));var b=XMLDOMAdapter().parseXml(a);var c=b.documentElement.getElementsByTagName('div');var d=false;for(var i=0;i<c.length;i++){if(c[i].attributes.getNamedItem('id')&&c[i].attributes.getNamedItem('id').value=='content'){d=DeepCopyNode(c[i]);i=c.length}}if(d){try{document.getElementById('content').parentNode.replaceChild(d,document.getElementById('content'))}catch(e){window.location=f}}else{window.location=f}}};request.send(null)} 
    links.js:

    PHP Code:
    var links document.getElementsByTagName("a");
       for(var 
    i=0i<links.lengthi++){
         if(
    links[i].className == "ajax"){
         
    links[i].onclick = function(){
         
    SendRequest(this);
         return 
    false;
         }
         }
       } 
    Any questions, just ask.


  2. #2
    likes chips. Scriptage's Avatar
    Join Date
    May 2008
    Location
    Yorkshire
    Posts
    2,757
    Right the forum's being gay again and removing half the code so the files are attached as text files, just rename to .js.
    Attached Files

  3. #3
    Forum Sniper™ blueocto's Avatar
    Join Date
    Oct 2007
    Location
    Newcastle upon Tyne
    Posts
    5,246
    Nice one Scriptage!

    I have a question *raises hand* several times people have mentioned that avoiding JS is beneficial, incase visitors have it switched off - would this effect this layout, and if so, what would the visitor see if it did?

  4. #4
    likes chips. Scriptage's Avatar
    Join Date
    May 2008
    Location
    Yorkshire
    Posts
    2,757
    No the whole point of this is that it is accessble; if Javascript is turned off the link in the href is opened so the user will see the file. Each file should follow the same template so each document looks the same; the only part that changes is the content (the part that we will get with AJAX), so there is a seemless transformation for those that do not have Javascript enabled.

  5. #5
    Forum Sniper™ blueocto's Avatar
    Join Date
    Oct 2007
    Location
    Newcastle upon Tyne
    Posts
    5,246
    Oh well that is reasurring. Thanks!

  6. #6
    likes chips. Scriptage's Avatar
    Join Date
    May 2008
    Location
    Yorkshire
    Posts
    2,757
    No problem :)

  7. #7
    Netvibes is an Addiction™ Toon's Avatar
    Join Date
    Jan 2007
    Location
    Sheffield, UK
    Posts
    23,638
    Blog Entries
    12
    whiskey makes everything accessible

  8. #8
    likes chips. Scriptage's Avatar
    Join Date
    May 2008
    Location
    Yorkshire
    Posts
    2,757
    I've learned that the hard way.

  9. #9
    AKA Whitley Golf Whitley Journalism's Avatar
    Join Date
    Mar 2008
    Location
    Leeds (UK)
    Posts
    1,532
    Quote Originally Posted by Scriptage View Post
    I've learned that the hard way.
    We all have.

  10. #10
    Dalek Lover Arkady's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow
    Posts
    2,732
    It seems like only yesterday that you were telling us all how much you hated frameworks in general and ajax in particular. Or did I dream that?

+ Reply to Thread
Page 1 of 3
1 2 3 LastLast

Similar Threads

  1. Create a Slick and Accessible Slideshow Using jQuery
    By Dunce in forum General Web Design Forum
    Replies: 0
    Last Post: 18-05-2009, 02:35 PM
  2. sIFR 2.0: Rich Accessible Typography for the Masses
    By ianm in forum Typography Forum
    Replies: 2
    Last Post: 08-01-2009, 04:39 PM
  3. Accessible Links - The Quick Guide
    By Graphic Design Links in forum Graphic Design Links
    Replies: 0
    Last Post: 10-07-2008, 11:10 AM
  4. The Importance of Accessible and Usable Design
    By Graphic Design Links in forum Graphic Design Links
    Replies: 0
    Last Post: 03-04-2008, 02:41 PM
  5. Styling Usable and Accessible Web Forms
    By Harry in forum Graphic Design Tutorials
    Replies: 1
    Last Post: 25-01-2008, 11:55 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


The Graphics Forum Web Design Stuff Free Decent Downloads Free Quality Wallpapers Graphics Forum Free Vista Themes
The Top The Best Images Tech Talk 247 Logo Design - $149 Affordable Stock Vector Illustrations Creativecurio - Design Blog Graphic Design Advertising

Check the forum often for the latest design announcements. Everything from graphic design and web design, to films and music. Estetica is a great place for people to get together and help each other out.



Web Hosting - UK Web Hosting services for business or personal website hosting needs.

Dedicated Servers - A full range of Managed Dedicated Server solutions suitable for all your requirements.

Graphic Design Blog | Web Design Forum | Graphic Design and Print Forum | Graphic Design Links | Advertise On This Site

Web Design UK | Vision.To Design | Leaflet Printing | Estetica Design Forum's Privacy Policy

Flyer Printing | Photography Blog | Design Forum Links | Logo Design | Graphic Design Social Network | Logo Design

Graphic Design Tutorials | Logo Designer | UK Logo Design Studio | Land for sale | Vector Art Blog | Leaflet Printing

Free Web Hosting | Custom Logo Design - $149 Only | Affordable Print Design Templates | Small Business Logo Design | Company Logo Design

Logo Design Service | Logo Design Firm | Logo Design Reseller | Custom Logo Design | Letterhead Printing | Flyer Printing | Business Card Printing

Printing | Leaflet Printing | Online Backup | T-Shirt Printing | Personalised Mugs | Canvas Printing | Free Web Hosting Comparison Site