OffBeatMammal

Searching for monkeys in Cyberspace

JavaScript - always something more to tweak

clock November 7, 2008 21:10 by author offbeatmammal

Thanks to a bunch of projects over the years I've learnt one or two handy tricks to make sure web pages load quickly, but there's always something new to learn!

In the past I've always tried to delay loading JavaScript files until the very end because most browsers block the loading of other files when they hit a <script type="text/javascript" src="xyz.js"> tag ... I guess it makes sense as the script being loaded may make some fundamental changes to the page layout.

But what if you do have a load of scripts that you need to load, but they won't really be relevant until the page is displayed anyway (ajax helper libraries, entry validation etc)?

Well, you can fool your browser into loading them just like ordinary files outside the control of the JavaScript engine - and the great thing is that the code is pretty simple and flexible (which was good as I had to drop it into a site where I've not looked at the code in over two years but other folks have added a bunch of scripts and couldn't work out why the page load had got significantly worse!

 

function loadJavaScriptAsync()
{
var args = arguments.length;
for (var i=0;i<args;i++)
{
document.write("<script src='" + arguments[i] + "'></" + "script>");
}
}
 
....
 
loadJavaScriptAsync(
"JSfile1.js",
"JSfile2.js",
"http://remoteserver.com/scripts/JSfile3.js",
"jsFile4.js");
 


Turning Facebook inside out

clock January 31, 2008 07:18 by author offbeatmammal

At little late with the news (but Michael's posting prompted me to follow up) - it looks like Facebook have made another one of their leaps to keep the other Social Networks wondering what they need to do to keep up.

Last year Facebook opened up their platform. It gave app developers the chance to write and deploy viral apps that any Facebook user could add to their profile and interact with. Sadly that has resulted in an increase in Facebook spam (hint: I don't want to battle your virtual vampire or send you a pixilated pina colada) but over time hopefully ranking of apps and increased user awareness (just like for any other sort of spam) we'll start to see some really useful apps turning up on the platform.

Well, they've now taken it a step further. They have opened up a JavaScript driven API that allows anyone with an HTML page to embed a Facebook app on their site.

Essentially it means that as a site developer you now have access to the Facebook infrastructure to build your own niche social network - saving you time and effort, giving you a robust, scalable platform, and giving Facebook potential new users as people sign up to take advantage of your network.

TechCrunch call it a clever move. I'm keeping my eyes peeled for the first really cool use of this extension to the platform.



Random MIX Bling!

clock December 3, 2007 12:16 by author offbeatmammal

As we get closer to MIX08 in Vegas I wanted to add some bling to my blog to remind visitors it was happening, but I couldn't choose which image to use.

Now one solution would be to roll a quick Silverlight app to grab the images and display them randomly with pretty transitions and the like (in fact that's something that the new Vertigo Slide.Show might be perfect for) but I went old school with one line of Javascript so I could get something up and running quickly

   1:  <script type="text/javascript">
   2:  document.write('<br><a href="http://visitmix.com/2008"><img src="http://visitmix.com/2008/images/bling/blings_9_25_' + String.fromCharCode(97 + Math.round(Math.random() * 14)) + '.jpg" border="0" alt="Visit MIX08"></a>');
   3:  </script>

It's quick and simple so feel free to steal and share ;) or you can just grab the images from FrankArrs page or the VisitMIX Mixtify site and play your self.



MaxLength on a Textarea

clock October 26, 2006 11:14 by author OffBeatMammal

Although HTML is very clever (and there are lots of very clever things hidden if you go looking) there's one thing that's bugged me since I first started putting forms together in a webpage.

Why is there a maxlenth parameter on an <input type="text"....> field but no maxlength attribute on a <textarea....>....</textarea>

Now I know it's possible to write some javascript and attach it to a text area that you want and control user input that way. But it requires thought and effort and it's not a very elegant solution.

But, as you can see... the problem can be solved:

I came across the concept of CSS behaviours (or is that behaviors) thanks to a really handy script 'behaviours.js' from Ben Nolan which allows you to assign a behaviour to a CSS rule. I then applied that to allow me to add a couple of extra attributes to the Textarea HTML tag. Specifically the Maxlength and showremain function. Adding them to a page is as easy as this (the hardest bit it to remember to include the two scripts!)

<html><head><script type="text/javascript" src="/scripts/behaviour.js"></script><script type="text/javascript" src="/scripts/textarea_maxlen.js"></script></head><body><p>Limit 120 Characters: <TEXTAREA rows="5" cols="30" maxlength="120" showremain="limitOne"></TEXTAREA> <br>Chars Remaining:<span id="limitOne">--</span></p><p>Limit 50 Characters: <TEXTAREA rows="2" cols="30" maxlength="50" showremain="limitTwo"></TEXTAREA> <br>Chars Remaining:<span id="limitTwo">--</span></p></body></html>

 The logic to check the length of the selected object, enforce it and (if required) update the span showing the remaining characters is within the textarea_maxlen script. It registers functions against the onkeydown, onkeyup, onpaste and onblur methods for the CSS textarea attribute.

var CSSrules = {
'textarea' : function(element){
element.onkeydown = function(event){
return doKeyPress(element,event);
}
,
element.onpaste = function(){
return doPaste(element);
}
,
element.onkeyup = function(){
return doKeyUp(element);
}
,
element.onblur = function(){
return doKeyUp(element);
}
}
}
 
Behaviour.register(CSSrules);

the actual javascript to check and enforce the length and update the progress is all pretty standard stuff - in fact, it's probably not that elegant but it was more a case of getting it working in a hurry and having it work in IE (PC), FireFox (PC, OSX and Ubuntu) and Safari (OSX) - the test platforms for the project (if you can tidy it up, please leave a comment here!) The biggest problem I encountered was the different event handling models for IE and Gecko based browsers - both in when events were triggered and how to stop them bubbling further!

var detect = navigator.userAgent.toLowerCase();// Keep user from entering more than maxLength characters
function doKeyPress(obj,evt){
maxLength = obj.getAttribute("maxlength");
var e = window.event ? event.keyCode : evt.which;
if ( (e == 32) || (e == 13) || (e > 47)) { //IE
if (maxLength && (obj.value.length > maxLength-1)) {
if (window.event) {
window.event.returnValue = null;
} else {
evt.cancelDefault;
return false;
}
}
}
}
function doKeyUp(obj){
maxLength = obj.getAttribute("maxlength");
if (maxLength && obj.value.length > maxLength){
obj.value = obj.value.substr(0,maxLength);
}
sr = obj.getAttribute("showremain");
if (sr) {
document.getElementById(sr).innerHTML = maxLength-obj.value.length;
}
}
// Cancel default behavior and create a new paste routine
function doPaste(obj){maxLength = obj.getAttribute("maxlength");
if (maxLength){
if ((window.event) && (detect.indexOf("safari") + 1 == 0)) { //IE
var oTR = obj.document.selection.createRange();
var iInsertLength = maxLength - obj.value.length + oTR.text.length;
try {
var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
oTR.text = sData;
}
catch (err) {
}
if (window.event) { //IE
window.event.returnValue = null;
} else {
//not IE
obj.value = obj.value.substr(0,maxLength);
return false;
}
}
}
}

In order for this to work in your own page remember: you must go and download the behaviour.js file from Bens site, and the textarea_maxlen.js script from here.

With a bit more imagination and time you could extend this to enforce rules like MaxLength="500" MaxWords="150" to lock down how much data can be entered, or MaxCaps="15%" to stop them SHOUTING.

The maxlength attribute is part of a proposed HTML5 standard, but it'll be interesting to see if we ever get there with the directions devolving to XHTML and microformats an rich internet application technologies such as Silverlight.



Have I mentioned... I hate the EOLAS patent

clock May 8, 2006 00:27 by author OffBeatMammal

I think I may have mentioned how stupid the Eolas Patent is, and what a pain it's going to be for developers who use embeded objects in a web page.

Luckily so far the scourge has only affected IE users so far. But hey, as that's 80-90% of the market depending on who you listen to that's enough to mean that there's a lot of web pages out there that need fixing.

Luckily the fix isn't a huge task, but it's a pain to have to do it... especially if you have a page with a lot of objects on.

Take this simple example:
<embed name="clicker" autostart="false" height="0" width="0" loop="false" src="/content/click.mp3" mce_src="/content/click.mp3" enablejavascript="true" CONTROLLER="true" /></embed>
apparently that is an offending ActiveX (apparently it falls foul of the "controls which require user interaction" clause in this misbegotten settlement)

So... what's the solution...

  • First of all, you have to wrap your code in a document.write like so:
    document.write('<embed name="clicker" autostart="false" height="0" width="0" loop="false" src="/content/click.mp3" mce_src="/content/click.mp3" enablejavascript="true" CONTROLLER="true" /></embed>');
  • Then save it in an external file (clicker.js)
  • Then reference it in the original document
    <script type="text/javascript" src="clicker.js" mce_src="clicker.js"></script>
Simple, but essentially pointless and time consuming.


Search

Calendar

<<  May 2013  >>
SuMoTuWeThFrSa
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

Sign in

Twitter


    follow OffBeatMammal at http://twitter.com



     
    Donate unused CPU cycles with BOINC Stats and Account Management from BOINCStats.com



    Blogroll

    Archive

    Tags

    Categories


    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2013