Crosstips.org

My fun Crossword solver project. Crosstips.org & Krysstips.se

Kung Fu

Fujian White Crane Kung Fu

Fry-IT

Fry-IT is the company I work for

Photos

Photoalbum, both old and new.

Zope

What I have and am doing with Zope

Receptsamlingen

In Swedish only. About my "Collection of Recipes" website.

Contact me

My contact details and how to contact me.

 

KungFuPeople.com
Do you train Kung Fu?
Or know someone who does?
Then check out KungFuPeople.com


Mobile version of this page Mobile version of this page

RSS

Hot topics

by jazz: btw whats the thought behind hiding captcha until user tried to add a comme...

Lesson learnt with creating DOM element with jQuer

by jazz: --- you saved my day!! thanks man...

Lesson learnt with creating DOM element with jQuer

by ramesh: Hi Quiquelie, Your code is cool thanks :) i was literally searching for onc...

setAttribute('style', ...) workaround for IE

by : good...

Lesson learnt with creating DOM element with jQuer

by Messias: Cool!! thxs!...

setAttribute('style', ...) workaround for IE

by Frank: Nice !!! Thx...

setAttribute('style', ...) workaround for IE

by Marko Sacher: Hi, how is this? element.style[k]= v; instead of eval(...)...

setAttribute('style', ...) workaround for IE

by Quiquelie: Hi, Try this: var closer = document.createElement('a'); a.style.float=...

setAttribute('style', ...) workaround for IE

by andre: My guess why "<div>" worked and "<a>" didn't is that "div" tags can be nest...

Lesson learnt with creating DOM element with jQuer

by whatispunk: Ever try using <a/>? Does that work?...

Lesson learnt with creating DOM element with jQuer

Old entries


September, 2009
London Frock Exchange launched
My first Twitter app - KungFuPeople.com
Comparing jsmin and slimmer
Python Code Dojo London - 17 Sep 2009
"Hello John. It's Gordon Brown."
7 of the World's Most Irresponsible Companies

August, 2009
Cgunit - Online Gallery
To sub-select or not sub-select in PostgreSQL
Custom CacheMiddleware that tells Javascript a page is cached in Django
What a super user-friendly menu!
Table Of Countries Showing Drive Direction
The Secret to SEO Search Engine Optimization
Calling all kung fu people - kungfupeople.com
Google Reverse Geocoding vs. GeoNames
gg - wrapping git-grep
Public calendars on Google Calendar
More optimization of Peterbe.com - CSS sprites

2009
2008
2007
2006
2005
2004
2003

 

You're viewing blogs from JavaScript only.

View all different categories

12th of March

Too much Python makes Peter a shit Javascript developer

This murdered a good half hour of my time splattered with lots of alert() statements to debug. Basically, in Firefox you can do this:

 var word = "Peter";
 alert(word[1]); // "e" in Firefox, undefined in IE

This is the wrong way to get to character in a string in Javascript. The correct way is to use charAt() like this:

 var word = "Peter";
 alert(word.charAt(1)); // "e" in Firefox and IE

I don't know about the other browsers but finally Crosstips.org now works in IE7 too. I haven't even looked at it in IE6 and don't intend to either.

24th of February

To $('#foo p') or to $('p', $('#foo'))

For the performance interested jQuery users please check out this thread

For the impatient, read Stephens reply He benchmarked what I asked and concluded that $("p", $("#foo")) is much faster in jQuery 1.3.2. I've been coding this style in jQuery for all recent projects so I'm happy with this outcome.

UPDATE

John Resig himself joined in on the discussion and had this to say:

"You should always use $("#foo").find("p") in favor of $("p", $("#foo")) - the second one ends up executing $(...) 3 times total - only to arrive at the same result as doing $("#foo").find("p")."

UPDATE 2

Not only did John join in on the discussion but it also made him work on jQuery 1.3.3 (not yet released at the time of writing) so that it doesn't matter which format you use you get the same performance. See the benchmark here

16th of January

Formatting numeric amounts in Javascript

Dear Lazyweb,

Is there a better method than this to format numeric amounts? Here's a solution I picked up from somewhere and slightly modified. It's heavily string based but passed the tests:

 function format_amount(i) {
   if(isNaN(i)) { i = 0.00; }
   var minus = '';
   if(i < 0) { minus = '-'; }
   i = Math.abs(i);
   i = parseInt((i + .005) * 100);
   i = i / 100;
   s = new String(i);
   if(s.indexOf('.') < 0) { s += '.00'; }
   if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
   s = minus + s;
   return s;
 }

The "tests" are:

 format_amount(100)       == "100.00";
 format_amount(100.0)     == "100.00";
 format_amount(100.05)    == "100.05";
 format_amount(100.051)   == "100.05";
 format_amount(-100)      == "-100.00";
 format_amount(-100.0)    == "-100.00";
 format_amount(-123.45)   == "-123.45";
 format_amount(-123.450)  == "-123.45";

So functionally it's OK but I'm not sure it's the best way to do it.

3rd of April

Lesson learnt with creating DOM element with jQuery

This took me some seriously wasted time to figure out yesterday. What I was trying to do was to create a DOM element of tag type A and insert it into the DOM tree of my page. As I was coding along, everything was working just fine in Firefox but the damn thing wouldn't show up anywhere in IE 6. I debugged and debugged and tried all kinds of different approaches and I just couldn't work it out. Then Karl Rudd gave the right hint on the jQuery mailing list.

Basically, what I was doing was something like this:

 var a = $("<a>").attr('href','#').click(somefunction);
 $('#toolbar').append(a);

What was then so strange is now less surprising. When I changed the <a> to a <span> it actually worked but just looked wrong with the rest of the site I was working on. Here's the correct way of doing it:

 var a = $("<a></a>").attr('href','#').click(somefunction);
 $('#toolbar').append(a);

Notice the difference between <a> and <a></a>. The strange thing is that to reproduce this I created this test.html page but here I noticed that in IE 6 it won't let you add any elements that are enclosing ones that are written as singulars. That's really strange since in the same javascript as the above stuff I did a $("<div>") which was working fine. I'll have to get back to figuring out why that one worked nad the A one didn't.

11th of January

input/textarea switcher with jQuery

Here's a very early version of a solution to a problem where you have an input box want to give the user the option to expand the box to a textarea if they want to enter more stuff such as multiple line content. Your implementation, when you attempt the same thing, might be differently but feel free to copy this as a good start for your own projects. The demo show how it works.

What was important for me in doing this was that I didn't want to get close to the XHTML at all since (in this particular case) it was generated from a widget mechanism and I wanted the expanding option a luxury only for those who bother with the full Javascript. The key solution for me was the ability to replace elements in the DOM tree and copy the attributes when going from input to textarea or the other way around.

Feedback welcomed. Bare in mind that this was a quick first attempt and that I haven't tested this on IE.

8th of January

jQuery and Highslide JS

If you use the wonderful Javascript library jQueryand the wonderful (standalone) Javascript plugin Highslide JS of recent version you should be aware of something.

As of recent versions of Highlide the way the Expander function works is that it looks at an element's onclick attribute and not it's attached events which means that if a DOM element has the event but not the attribute you get a Javascript error. In older versions of Highslide you were able to do this:

 $('a.highslide').click(function() {
    return hs.expand(this, options);
 });

But that's no longer working since the attribute isn't set. Here's the new way of doing it:

 $('a.highslide').each(function() {
    this.onclick = function() {
      return hs.expand(this, options);
    };
 });

19th of December

isArithmeticExpression() in Javascript

Following from some work that I blogged about two days ago (Calculator in Python for dummies) I've now extended the functionality thinking into the AJAX scripts that sit on top of this Python server-side functionality. How this was implemented is boring but the following function helped me a lot. Here's the code with a very basic unit test after:

 function isArithmeticExpression(s) {
   return /[\d]\s*\+\s*[\d]|[\d]\s*\-\s*[\d]|[\d]\s*\/\s*[\d]|[\d]\s*\*\s*[\d]|[\d]\s*\^\s*[\d]/.test(s) &&
         s.split(/\)/).length == s.split(/\(/).length &&
         !/[A-Za-z_]/.test(s);
 }

 function assert(fact) {
   if (!fact) alert("Assert failure!");
 }
 assert(isArithmeticExpression('') == false);
 assert(isArithmeticExpression('++123') == false);
 assert(isArithmeticExpression('+123') == false);
 assert(isArithmeticExpression('2+123') == true);
 assert(isArithmeticExpression('2 + 123') == true);
 assert(isArithmeticExpression('2 + - 123') == false);
 assert(isArithmeticExpression('2 + 123') == true);
 assert(isArithmeticExpression('(2 + 123)') == true);
 assert(isArithmeticExpression('2^6') == true);
 assert(isArithmeticExpression('(2+1))^6') == false);
 assert(isArithmeticExpression('a+123') == false);
 assert(isArithmeticExpression('1a1+2x3') == false);

Basically, it returns true if the string appears to contain only numbers and one of the expected operators +, -, *, / or ^ in between two numbers.

It's far from perfect. I can think of cases where it will actually fail. But those cases are very rare and are too unlikely to happy and cause a major problem in this application and I'd rather get on with it than to spend any more time on this. After all, this is just a Javascript that tries to help if it can. The server-side code needs to "perfect" and if someone enters a weird expression, the server-side error handling will at least pick it up.

22nd of November

Note to self about Jeditable

I've been struggling hard this morning to get Jeditable to work in IE (6 and 7). Whilst everything was working perfectly fine in Firefox, in IE the clickable editable text would just disappear and never return. The solution was to use the latest jQuery 1.2.1. I was using version 1.1.4 which was why it didn't work.

Jeditable is a brilliant plugin with really good configuration options (hint read the source code's documentation comment) and I'll now send an email to Mika about this pitfall and suggest that he includes it in his documentation.

18th of September

Vertically expanding textarea input boxes

I've recently improved the IssueTrackerProduct so that when you start to write in the little textarea it expands and grows vertically as the text gets larger and larger. Other sites like Highrise do this too for note taking.

Long story short, here's the demo and here's the solution:

 function _getNoLines(element) {
   var hardlines = element.value.split('\n');
   var total = hardlines.length;
   for (var i=0, len=hardlines.length; i<len; i++) {
      total += Math.max(Math.round(hardlines[i].length / element.cols), 1) - 1;
   }
   return total;
 }

 $(function() {

   // First, for all the textareas that have lots of lines of text 
   // in them, we want to double their number of rows
   $('textarea.autoexpanding').each(function() {
      while (_getNoLines(this) > parseInt(this.rows))
        this.rows = '' + Math.round((parseInt(this.rows) * 1.5));
   });

   // When a user enters new lines, if they have entered more
   // lines than the textarea has rows, then double the textareas rows
   $('textarea.autoexpanding').bind('keyup', function() {
      if (_getNoLines(this) > parseInt(this.rows))
        this.rows = '' + Math.round((parseInt(this.rows) * 1.5));
   });

 }


>Read the whole text (71 more words)

8th of January

setAttribute('style', ...) workaround for IE

I knew had I heard it before but I must have completely missed it anyway and forgotten to test my new Javascript widget in IE. None of the stylesheet worked in IE and it didn't make any sense. Here's how I did it first:

 var closer = document.createElement('a');
 a.setAttribute('style', 'float:left; font-weight:bold');
 a.onclick = function() { ...

That worked in Firefox of course but not in IE. The reason is that apparently IE doesn't support this. This brilliant page says that IE is "incomplete" on setAttribute(). Microsoft sucked again! Let's now focus on the workaround I put in place.

First I created a function to would take "font-weight:bold;..." as input and convert that to "element.style.fontWeight='bold'" etc:

 function rzCC(s){
   // thanks http://www.ruzee.com/blog/2006/07/\
   // retrieving-css-styles-via-javascript/
   for(var exp=/-([a-z])/; 
       exp.test(s); 
       s=s.replace(exp,RegExp.$1.toUpperCase()));
   return s;
 }

 function _setStyle(element, declaration) {
   if (declaration.charAt(declaration.length-1)==';')
     declaration = declaration.slice(0, -1);
   var k, v;
   var splitted = declaration.split(';');
   for (var i=0, len=splitted.length; i<len; i++) {
      k = rzCC(splitted[i].split(':')[0]);
      v = splitted[i].split(':')[1];
      eval("element.style."+k+"='"+v+"'");

   }
 }

I hate having to use eval() but I couldn't think of another way of doing it. Anybody?

Anyhow, now using it is done like this:

 var closer = document.createElement('a');
 //a.setAttribute('style', 'float:left; font-weight:bold');
 _setStyle(a, 'float:left; font-weight:bold');
 a.onclick = function() { ...

and it works in IE!