Hiding elements when javascript is enabled

I was cleaning up some javascript notifications for a website and came across this common question: what is the best way for elements to be visible when a user does not have javascript enabled but hidden when they do (often to be revealed by javascript?

I cam across an article titled How To Hide And Show Initial Content, Depending On Whether JavaScript Support Is Available which was great reading on the subject.  There were 3 methods that stood out in the article and comments.

  1. Use a javascript file sourced in the head section to include a css stylesheet file specifically for javascript enabled browsers.  The downsides of this method are 2 extra requests on the initial visit and needing to keep all javascript css in a separate css file.
  2. A method detailed in The best way to hide content by JavaScript is to attach a class to the html element.  This is a violation of the spec and therefore may not be cross browser compatible but seems to be used by a number of people without problem.
  3. By adding a 1 line script immediately after the body tag that sets the body class to “js” or something else indicating the presence of javascript.  The chief objection to this is that setting the body class could trigger a re-rendering.

At the moment I’m trying option 3.  My reasoning is that it is the most simple to implement and maintain.  The concern that it could cause a re-rendering seems minimal to me.  Presumably since the parser proceeds linearly through the html than this change should occur before much else and thus not lead to any visible flicker.  However, real world testing may be the only way to discern if any of these methods are superior to the others.

Ruby Arrays and Hashes and Days of the Week.

I was playing around with different ways to represent the days of the week and their index from 0 to 6 as hashes and arrays.  Here’s what I cam up with:

irb(main):001:0> require 'date'
=> true

irb(main):002:0> Date::DAYNAMES
=> ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

irb(main):003:0> Date::DAYNAMES.enum_with_index.to_a
=> [["Sunday", 0], ["Monday", 1], ["Tuesday", 2], ["Wednesday", 3], ["Thursday", 4], ["Friday", 5], ["Saturday", 6]]

irb(main):004:0> Date::DAYNAMES.enum_with_index.to_a.map{|a| a.reverse}
=> [[0, "Sunday"], [1, "Monday"], [2, "Tuesday"], [3, "Wednesday"], [4, "Thursday"], [5, "Friday"], [6, "Saturday"]]

irb(main):005:0> Hash[*Date::DAYNAMES.enum_with_index.to_a.flatten]
=> {"Friday"=>5, "Wednesday"=>3, "Saturday"=>6, "Tuesday"=>2, "Monday"=>1, "Sunday"=>0, "Thursday"=>4}

irb(main):006:0> Hash[*Date::DAYNAMES.enum_with_index.to_a.flatten].invert
=> {5=>"Friday", 0=>"Sunday", 6=>"Saturday", 1=>"Monday", 2=>"Tuesday", 3=>"Wednesday", 4=>"Thursday"}

Blocking Spam with Javascript

So I am trying the WP-SpamFree Anti-Spam plugin for WordPress.  It uses javascript to prevent spam.  I’m really hesitant because I prefer websites which do not require javascipt for their basic functionality.  Perhaps I will use another spam blocker.  Or perhaps I will just resign myself to accepting that functional javascript is requirement from browsing the web.

Update: I’m going to try the NoSpamNX plugin instead.  It does not require cookies or javascript.  We’ll see how it goes.

2 ways to use LIKE queries with wildcards safely in Rails finders

It is important to sanitize variables that may come from users to prevent SQL injection attacks.  Rails makes this easy by default:

Author.find(:all, :conditions=>['first_name = ?', first_name]

However this will not work:
Author.find(:all, :conditions=>['first_name LIKE "%?%"', first_name]

This will work but is  insecure:
Author.find(:all, :conditions=>"first_name LIKE '%#{first_name}%'"

Solution 1:
Author.find(:all, :conditions=>['first_name LIKE ?', "%#{first_name}%"]

Solution 2:
Author.find(:all, :conditions=>['first_name LIKE CONCAT("%", ?, "%")', first_name]

Running Xen on Various Linux Distributions

I have been looking to various GNU/Linux distributions to determine their level of support for Xen.  Here’s what I found:

  • Debian Etch – Xen Hypervisor 3.2.1 (via backports), Linux kernel 2.6.26 (via backports)
  • Debian Lenny – Xen Hypervisor 3.2.1, Linux dom0 kernel 2.6.26
  • Debian Squeeze (testing) – Xen Hypervisor 3.4.2, Linux dom0 kernel ???
  • Ubuntu 8.04 (Hardy) – Xen Hypervisor 3.3.0 (via backports), Linux dom0 kernel 2.6.24
  • Ubuntu 8.10-10.04 (Intrepid-Lucid) – Xen Hypervisor 3.3.0, no Linux dom0 kernel
  • OpenSuse 11.2 – Xen Hypervisor 3.4.1, Linux dom0 kernel 2.6.31

As much as I like Debian and Debian based distros I think OpenSuse is probably the best platform for hosting Xen at the moment.

Fixing pygrub in Ubuntu 9.04

I think Ubuntu 9.04 is the only version of Ubuntu suffering from this bug but it appears as though no fix for this serious regression will be forthcoming.  To get pygrub to work you actually have to fix a bug in the python 2.5 curses library.  Open “/usr/lib/python2.5/curses/__init__.py” and add the following lines starting at line 17:

import os as _os
import sys as _sys

After that pygrub should work correctly.  More information about this bug can be found in launchpad bug #395321.

FileZilla sends spaces in user name fields

Just got done helping a user debug connection problems using FileZilla to connect over SFTP/SSH.  I noticed, sadly not immediately, that the username he was using was being send with an extra space at the end and thus was an invalid user.  I’m not sure there is a valid reason not to trim/strip whitespace from the beginning and ending of a username.  But this is something to be aware of.

Open Source Search Engines with Web Crawling and HTML Indexing

The term search engine is particularly vague.  It can mean an online search engine, or it can mean software that provides something similar to the online search engines, or it can mean software that does fulltext indexing and querying.  I specifically wanted to find a tool that could be used to create a niche search engine without having to write the whole thing myself as a ruby on rails app.  Here’s what I found:

  • mnoGoSearch – seems very actively maintained, packages present in Debian and Ubuntu, written in c
  • DataparkSearch – seems fairly actively maintained, apparently a branch of mnoGoSearch, written in c
  • Nutch – seems fairly actively maintained,written in java, built on top of Lucene

And that’s really it.  There is a product called ASPseek but it has long since been abandoned.  There is also ht://Dig but it has not been updated in years and is not really in the same league as the above.  So, at least for anyone trying to research this area there are not a whole lot options to try.

Continue reading ‘Open Source Search Engines with Web Crawling and HTML Indexing’ »

Setting up a user with only scp and sftp access

In the olden days one had to use packages such as scponly and rssh in order to restrict a user account to just being able to use scp and sftp.  Now that functionality is built into OpenSSH.  A client wanted me to setup such an account for 1 user on a system.  I modified sshd_config like this:

Match user username
  ChrootDirectory /home/%u
  ForceCommand internal-sftp
  X11Forwarding no
  AllowTcpForwarding no

Note that this only works in Ubuntu 8.10 and later and Debian 5.0 and later.  Also, be aware that the home directry has to be owned by root and not writable by the user for security reason.  So a directory inside the home directory will need to be created for the user to upload any files.

Continue reading ‘Setting up a user with only scp and sftp access’ »