My musings while programming in a number of languages and libraries, from PHP, Perl, JavaScript, jQuery, and KnockoutJS, with liberal use of CSS and Bootstrap to make it all pretty.
2015-02-27
jQuery: Counting the number of matching class elements on the page
While working on a project I hacked some code together to count the number of flagged questions in an exam. My initial code, which is bad and poorly optimized as seen here:
While here I have my optimized code:
Not only is the optimized code smaller (nearly half as many lines and its also much easier to read.
Labels:
JavaScript,
jQuery
Location:
Milwaukee, WI, USA
2015-01-08
jQuery: Hot Keys!
A while ago we added a button on one of our pages that takes you to the next logical place on the page. This button over time has moved and is far from the inputs it needs to take you to. The next logical enhancement for this button was to make it a hot key as well, so that is what I've done.
In addition to jQuery, which is already loaded on our pages we've added a small-ish (4.52KB, 1.95KB minified) library called, jquery.hotkeys.js. With a couple of lines I was able to add in a simple hot key combo that also takes you to the same logical spot on the page. This allows our users to keep their hands on the keyboard for better efficiency.
Example:
In addition to jQuery, which is already loaded on our pages we've added a small-ish (4.52KB, 1.95KB minified) library called, jquery.hotkeys.js. With a couple of lines I was able to add in a simple hot key combo that also takes you to the same logical spot on the page. This allows our users to keep their hands on the keyboard for better efficiency.
Example:
Labels:
JavaScript,
jQuery
Location:
Milwaukee, WI, USA
2015-01-06
jQuery: Finding the first input with a class
Just a quick little snippet for finding the first input on the page with a specific class.
Labels:
JavaScript,
jQuery
Location:
Milwaukee, WI, USA
2014-06-10
Converting Bytes to human readable Perl function
I'm sure this is covered by a Perl Module some where, but I haven't gone looking. Instead I've written and the re-written a function that will convert a raw number of bytes to something more human friendly.
Below you can see the two subroutines I've written over time.
The old one only went up to Petabyte (which is already larger than we will accept for a file). While usable it just seems clunky compared to the new function.
Below you can see the two subroutines I've written over time.
The old one only went up to Petabyte (which is already larger than we will accept for a file). While usable it just seems clunky compared to the new function.
Location:
Milwaukee, WI, USA
2014-04-29
jQuery adding options to a select list
From time to time I find myself searching for the same solution to something I know I've already done. This is one of those things. I needed to add options to a select list with jQuery.
In the code below lines one and four are the old manual way I did things. If I needed the value or the text of the option to be a JavaScript variable, I had to start concatenating the strings. This is a bit easier with lines two and five.
In the code below lines one and four are the old manual way I did things. If I needed the value or the text of the option to be a JavaScript variable, I had to start concatenating the strings. This is a bit easier with lines two and five.
Also note, in the programmatic way if you need to insert HTML entities you have to use the html function instead of the text function.
Labels:
HTML,
JavaScript,
jQuery
Location:
Milwaukee, WI, USA
2014-03-11
Ubic: A polymorphic service manager
A few years ago I wrote a simple perl script that looks to a Database and processes any jobs. It runs in an instance of screen that I keep running on a server. The script is a "while (1){...;sleep(30);}" type script. I wanted the script to run more frequently than every minute and I wanted to be able to keep track of its progress. For some reason I didn't want to write output to a log file or check the DB to ensure jobs had run as expected.
I'd really like to move away from that screen'ed script and move it to something that starts when the server reboots / and is kept running should anything go wrong with it (shockingly nothing has killed it to date). My mentor at work recently showed us Ubic in a presentation for doing just this sort of thing. So I've spent the morning studying Ubic, along with some of the blog entries for creating a simple Ubic service.
The worker script:
The service description:
Some sample output in the log file:
As you can see, its a very simple setup to replicate multiple worker threads that take an indeterminate amount of time to complete their jobs.
My next task is to actually move my original script out of screen and into Ubic for better resiliency and tracking. The other task I have ahead of me is monitoring Ubic to ensure my scripts remain running. Remember to "Trust, but verify"!
I'd really like to move away from that screen'ed script and move it to something that starts when the server reboots / and is kept running should anything go wrong with it (shockingly nothing has killed it to date). My mentor at work recently showed us Ubic in a presentation for doing just this sort of thing. So I've spent the morning studying Ubic, along with some of the blog entries for creating a simple Ubic service.
The worker script:
The service description:
Some sample output in the log file:
As you can see, its a very simple setup to replicate multiple worker threads that take an indeterminate amount of time to complete their jobs.
My next task is to actually move my original script out of screen and into Ubic for better resiliency and tracking. The other task I have ahead of me is monitoring Ubic to ensure my scripts remain running. Remember to "Trust, but verify"!
Location:
Milwaukee, WI, USA
2014-03-10
CSS tag parsing in Perl
Just a quick one here, haven't played enough yet to fully expand on this.
Mojo::DOM
It let's you do CSS style tag parsing in Perl.
For example, where $content is an HTML document:
Mojo::DOM
It let's you do CSS style tag parsing in Perl.
For example, where $content is an HTML document:
Subscribe to:
Posts (Atom)