Kung FuKung Fu

Fujian White Crane Kung Fu

ZopeZope

What I have and am doing with Zope

PhotosPhotos

Photoalbum, both old and new.

ReceptsamlingenReceptsamlingen

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

Contact meContact me

My contact details and how to contact me.

  Mobile version of this page Mobile version of this page

RSS

Hot topics

by agent47: Wow that was great buddy ! You saved me a lot of weeks. I need to personall...

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

by state-machine: Yes, this is a really short solution! Thank you all for not giving up on th...

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

by Matt: Just what I was looking for. Thanks a lot!...

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

by tjallen: Hooray! Thank-you. In my case, involving styles of a div element, this wo...

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

by Ra: BEFORE var closer = document.createElement('a'); //a.setAttribute('style'...

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

by Brittany: thanks, it works well!...

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

by Peter Bengtsson: Really? I'll give that a try....

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

by ivo van der Wijk: node.style.cssText = "..." usually does the trick for me...

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

Old entries


February, 2008
hostip.info - Look up the location from an IP
CommandLineApp by Doug Hellmann
If Americans knew - An interesting insight into the Israeli Palestine conflict
Chinese New Year and the Persecution of Falun Gong in China
logrotating all my Zope event logs
Ocado gets customer service right
Why Django and Grok matters

January, 2008
The Official Dilbert Widget
"lost my phone :("
Ugliest e-commerce site of the month - Comfy-Feet
input/textarea switcher with jQuery
jQuery and Highslide JS
The Love Mattress
EditArea vs. CodePress

2007
2006
2005
2004
2003

 

You're viewing blogs from JavaScript only.

View all different categories

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

http://www.peterbe.com/plog...-switcher/demo.html 

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

http://vikjavev.no/highslid...pic.php?p=5190#5190 

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!