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 : I like how you automatically escaped the newline in the commented code ;-P...

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

by DUzun: Hi, Lukas! Try putting that script in the body of the document....

jQuery and Highslide JS

by Peter Bengtsson: No unfortunately not. It just didn't work very well so I re-thought my app ...

input/textarea switcher with jQuery

by emi: Hi there, did you push this script any further? Seems interesting, but a bi...

input/textarea switcher with jQuery

by Chris: Works like a charm! I definitely needed this dynamic!! Thanks so much!...

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

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

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

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

Old entries


April, 2010
Word Whomp solvers love Crosstips
UPPER vs. ILIKE
Who was logged in during a Django exception
fcgi vs. gunicorn vs. uWSGI
Cycling across England on Orange Snapshot

March, 2010
The awesomest way possible to serve your static stuff in Django with Nginx
Beautiful photos from the Katrina hurricane
Speed test between django_mongokit and postgresql_psycopg2
How and why to use django-mongokit (aka. Django to MongoDB)
Ubuntu Cola or Ubuntu Linux
Importance of public URLs and how enterprisecarsales.com gets it wrong

February, 2010
January, 2010
2009
2008
2007
2006
2005
2004
2003

 

You're viewing blogs from JavaScript only.

View all different categories

1st of September

Local NodeJS development environment with Nginx

I'm brand spanking new to the node.js web application development. The framework I'm currently using is express which seems OK. So I've got an app that consists of 1 static HTML file, a lot of Javscript/CSS/image resources and some express GET and POST views that return small snippets of HTML. All data will be loaded with AJAX to avoid having to use any HTML templating on first load. What's cool about this is that it's soo fast! Everything except the JSON data can be loaded from an Nginx server.

At the moment I've got a light static HTML page that loads about 240Kb of Javascript and CSS (jQuery UI is big) and a couple of bytes of JSON data pulled from Node. As a little anal perfectionism I put an Nginx server in front so that Node doesn't have to serve any of the static files. To get that you have to have a Nginx site enabled that looks like this:

 server {
    root /home/peterbe/task-calendar/static;
    location / {
      if (-f $request_filename) {
          add_header X-Static hit;
          access_log   off;
      }
      if (!-f $request_filename) {
          proxy_pass http://127.0.0.1:8000; # where Node is running
          add_header X-Static miss;
      }
    }
 }

I think much of the fun of working with this app is that it's a delight to see it load in the browser without any sluggishness or delay. Lovely!

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!