Ajax Updates |
- AJAX Poller Script
- jQuery Spy : Jquery Plugin
- Chroma-Hash : Ajax Script
- Fit Text Into a Box : Ajax Script
| Posted: 12 Nov 2009 02:58 PM PST Configuration This script requires that you have PHP installed on your server and access to a mySql database. * ajax-poller.html = Main HTML file. Contains some HTML code and some PHP code. You will find the code for the poller between the comments <!-- START OF POLLER --> and <!-- END OF POLLER --> # ajax-poller-cast-vote.php = This file is called by ajax when someone casts a vote. This file updates the database and returns vote results as XML back to ajax-poller.html Installation This is a step by step guide on how to configure this script 1. Modify dbConnect.php. Insert your dbName, username and password. You may have to create a new database. Javascript variables You will find some variables at the top of ajax-poller.js which you could modify: var serverSideFile = 'ajax-poller-cast-vote-php'; var voteLeftImage = 'images/graph_left_1.gif'; var voteRightImage = 'images/graph_right_1.gif'; var voteCenterImage = 'images/graph_middle_1.gif'; var graphMaxWidth = 120; // It will actually be a little wider than this because of the rounded image at the left and right var graphMinWidth = 15; // Minimum size of graph var pollScrollSpeed = 5; // Lower = faster var useCookiesToRememberCastedVotes = true; * serverSideFile = Path to the PHP file that is called by ajax. ![]() Related Listings:
| |||||
| Posted: 12 Nov 2009 02:48 PM PST Following many requests, I have upgraded the jQuery spy code to support multiple items returned from the AJAX response and custom timestamp functions – so that requests can be completely tailored. Import the plug-in, and call the code on the spy container div (assuming you’ve imported jQuery already). <script type="text/javascript" src="spy.js"></script> <script type="text/javascript"> $(function() { /* returns a selection of HTML DIVs to be inserted in to the #spyContainer in a spy-style */ $('#spyContainer').spy({'ajax': '/ajax/news.php'}); }); </script> <div id="spyContainer"></div> Usage * ajax – required – the URL to server side script that will return the content For example: $(function() { $('#spyContainer').spy({ 'ajax': '/jquery_spy/out.php', // return type is HTML 'fadeInSpeed': '1400', // crawl 'timeout': 2000 // new item every 2 seconds }); }); Custom Functionality * push – custom push on function, this must be customised if using JSON Custom Push If you don’t want to return HTML to the spy, you can write your own push method. Your custom method will have access to ‘this’ which is the DOM element (i.e. the contain div) and ‘response’ which is the response in plain text. Here is an example using JSON: $(function() { $('#spyContainer').spy({ 'ajax': '/jquery_spy/out_json.php', 'push': custom_push }); }); function custom_push(response) { eval("var json = " + response); // convert to JSON // I'm being lazy here, but you get the idea: // Build the HTML var html = '<div style="display:none">' html += '<span class="ctr">' + json.num; html += '</span><span class="content">'; html += json.string + '</span></div>'; // Prepend the HTML inside the container $('#' + this.id).prepend(html); } Custom timestamp Most developers have their own system for tracking the latest items, so I had this in to it’s own pass in function. The example should explain the usage. The return value from the ‘timestamp’ value will be what is passed in to the AJAX request via the ‘timestamp’ posted value. $(function() { $('#spyContainer').spy({ 'limit': 25, 'fadeLast': 3, 'ajax': '/jquery_spy/out.php', 'fadeInSpeed': 'slow', 'timeout': 3000, 'timestamp' : myTimestamp }; }); function myTimestamp() { var d = new Date(); // formated as yyyy-mm-dd HH:MM:ss return d.getFullYear() + '-' + pad(d.getMonth()+1) + '-' + // because month starts at zero pad(d.getDate()) + ' ' + pad(d.getHours()) + ':' + pad(d.getMinutes()) + ':' + pad(d.getSeconds()); } function pad(n) { n = n.toString(); return n.length == 1 ? '0' + n : n; } Custom isDupe This function allows you compare the latest item against the last item to flag for a duplicate. This customisation really comes in to it’s own if you’re using JSON. If give the JSON object an ID, then you can compare the ID between the latest and the last items and chose not to show it. Continuing in the Digg spy fashion, this spy does allow duplicates to appear in the list, but not next to each other. If the return value is true the latest item is not shown. If the return value is false, the item is shown. The function takes two parameters: latest item and last item. Here’s an example using JSON (assuming our JSON output object has an ID attribute): $(function() { $('#spyContainer').spy({ 'limit': 25, 'fadeLast': 3, 'ajax': '/jquery_spy/out_json.php', 'fadeInSpeed': 'slow', 'timeout': 3000, 'method': 'json', // JSON output 'isDupe': myIsDupe }; }); function myIsDupe(latest, last) { return !!(latest.id == last.id); // return boolean } Custom pushTimeout The custom pustTimeout is particularly useful if you want to show all the items returned from the Ajax call to be pushed on to the stack straight away (rather than evenly one at a time). The best way to do this is: $(function () { $('spyContainer').spy({ ... normal settings ... 'pushTimeout': 1 // gives the appearance of all coming in at once }); }); Pause and Play Buttons Included in the plug-in are pause: pauseSpy() and play: playSpy() functions. These will, as the digg spy does, pause the flow of new content and start it up again. You can see these in use in the simple example I touched on this before, but I would strongly recommend that you think about those users that will have JavaScript disabled for whatever reason. Your spy page should still work. Here’s what I would recommend: 1. Pre-populate the page. If you are going to limit to 10 items in the spy, start off with 10 items on the page. Here’s an example: <script type="text/javascript"> // the index starts at zero - so fade down all // the divs after the 5th one $(function() { $('#spyContainer > div:gt(4)').fadeEachDown(); // initial fade $('#spyContainer').spy({ 'limit': 8, 'fadeLast': 3, 'ajax': '/jquery_spy/out.php', 'fadeInSpeed': 1400, 'timeout': 3500 }; }); </script> </noscript><noscript> <meta http-equiv="refresh" content="60" /> </noscript> Source: http://leftlogic.com/jquery/spy.html ![]() Related Listings:
| |||||
| Posted: 12 Nov 2009 02:32 PM PST Password entry can be frustrating, especially with long or difficult passwords. On a webpage, secure fields obscure your input with, so others can’t read it. Unfortunately, neither can you—you can’t tell if you got your password right until you click “Log In”. Chroma-Hash displays a series of colored bars at the end of field inputs so you can instantly see if your password is right. Chroma-Hash takes an MD5 hash of your input and uses that to compute the colors in the visualization. The resulting color pattern is non-reversible, so no one could know what your password just from the colors. Usage $("input:password").chromaHash({bars: 3, salt:"7be82b35cb0199120eea35a4507c9acf", minimum:6}); * bars number of bars displayed (1,2,3, or 4) Download: http://github.com/mattt/Chroma-Hash/archives/master ![]() Related Listings:
| |||||
| Fit Text Into a Box : Ajax Script Posted: 12 Nov 2009 02:20 PM PST You use this script by defining a < DIV > with an ID. This div should contain one < SPAN > tag which holds the text. Example:
Put this into your < HEAD > section <style type="text/css"> body{ font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif; } #theBox{ width:500px; border:1px solid #CCC; color: #317082; } #theBox2{ width:400px; border:1px solid #CCC; color:red; text-align:center; } #theBox3{ width:800px; height:50px; border:1px solid #CCC; color:red; text-align:center; } </style> <script type="text/javascript"> var fitTextInBox_maxWidth = false; var fitTextInBox_maxHeight = false; var fitTextInBox_currentWidth = false; var fitTextInBox_currentBox = false; var fitTextInBox_currentTextObj = false; function fitTextInBox(boxID,maxHeight) { if(maxHeight)fitTextInBox_maxHeight=maxHeight; else fitTextInBox_maxHeight = 10000; var obj = document.getElementById(boxID); fitTextInBox_maxWidth = obj.offsetWidth; fitTextInBox_currentBox = obj; fitTextInBox_currentTextObj = obj.getElementsByTagName('SPAN')[0]; fitTextInBox_currentTextObj.style.fontSize = '1px'; fitTextInBox_currentWidth = fitTextInBox_currentTextObj.offsetWidth; fitTextInBoxAutoFit(); } function fitTextInBoxAutoFit() { var tmpFontSize = fitTextInBox_currentTextObj.style.fontSize.replace('px','')/1; fitTextInBox_currentTextObj.style.fontSize = tmpFontSize + 1 + 'px'; var tmpWidth = fitTextInBox_currentTextObj.offsetWidth; var tmpHeight = fitTextInBox_currentTextObj.offsetHeight; if(tmpWidth>=fitTextInBox_currentWidth && tmpWidth<fittextinbox_maxwidth && tmpHeight<fitTextInBox_maxHeight && tmpFontSize<300){ fitTextInBox_currentWidth = fitTextInBox_currentTextObj.offsetWidth; fitTextInBoxAutoFit(); }else{ fitTextInBox_currentTextObj.style.fontSize = fitTextInBox_currentTextObj.style.fontSize.replace('px','')/1 - 1 + 'px'; } } </script> Put this into your < BODY > section <div id="theBox"><span>This is the content</span></div> <script type="text/javascript">fitTextInBox('theBox');</script> <br /> <div id="theBox2"><span>www.dhtmlgoodies.com</span></div> <script type="text/javascript">fitTextInBox('theBox2');</script> <br /> <div id="theBox3"><span>Max height of this box is set to 50px</span></div> <script type="text/javascript">fitTextInBox('theBox3',50);</script> ![]() Related Listings:
|
| You are subscribed to email updates from Ajax Updates To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
| Google Inc., 20 West Kinzie, Chicago IL USA 60610 | |


0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.