Sunday, 1 June 2014

VirtualBox -- USB not working

Just a snippet today, as much to remember it as anything else:

If you're using VirtualBox on Linux and USB devices aren't working despite the fact you've installed the extension pack, it's because you haven't remembered to add yourself to the vboxusers group.

To do that:

# usermod -a -G vboxusers your-user-name

...and then you'll probably want to log out and log back in so your UI processes pick up the change.

Thursday, 29 May 2014

Chrome and Java

If you use Chrome and are unfortunate enough to still have to use Java applets from time-to-time, you may recently have been unpleasantly surprised to find that the Java plug-in stopped working (if you use Linux), or you soon will be (if you use Windows; I don't know when/if this is happening on Macs).

Why? Because Chrome is discontinuing support for the venerable old NPAPI, the mechanism that the Java plug-in uses to hook into the browser, and Oracle hasn't released a Java plug-in using the newer PPAPI that Google touts.

"Does the browser tell you that?" you ask
No, it just acts like the plugin isn't installed at all.

"Not even if you enable the debug log with the verbose option?"
Nope.

"Then what does it do?"
It shows you a message saying you need Java and gives you a link to Oracle's website to download it, even though doing so will do you no good whatsoever.

"Isn't that...just really amazingly stupid?"
Oh yes.

Let's be clear: I'm not saying discontinuing support for the NPAPI is a bad idea, just that doing so in such a half-baked fashion is about has hamfisted as it gets. I wasted far too much time trying to figure out why Java suddenly stopped working when a simple "Chrome no longer supports the plug-in you have installed to handle this content" with a link to this page would have told me what was going on.

Which is why I'm telling you, dear reader. :-) And you might want to join me in telling them to be more clear.


Tangent: But we shouldn't be using Java applets anyway, right? Well, there are some who would argue that, and certainly Oracle's botched handling of Java on the client (and Sun's before them) has done a lot over the years to destroy applets, and better in-browser options have reduced the need for them. But those significant codebases won't just update themselves, and there are still things you can do in a Java applet (signed applet, of course) that you just can't do otherwise. One of my clients uses a VPN that launches via a signed Java applet, for instance. I can't just tell them to get lost because Google decided NPAPI was a bad thing. Google's own figures show that they're inconveniencing 9% of their users by doing this. That ain't nuthin. So applets are niche, but still there.

Monday, 17 March 2014

Session tokens, cookies, and local storage

Link-post today, the Meteor blog has a very interesting post about why Meteor has so-far avoided session cookies in favor of localStorage, including a high-level but nevertheless useful overview of issues with session cookies. Useful read for the security-minded, including an interesting suggestion near the end of a "both and" approach.

Tuesday, 18 February 2014

Centering Made Easy

Almost a note-to-self today. :-) Now that IE6 and IE7 are dead unless you need to support users from the far East, centering content within elements using CSS is dead easy. Three simple rules:

display:        table-cell;
text-align:     center;
vertical-align: middle;

So for instance:

<style>
  .content {
    width: 300px;
    height: 300px;
    border: 1px solid #aaa;
    padding: 2px;
    display: table-cell;
    text-align: center;
    vertical-align: middle;
  }
</style>
<div class="content">
    Here is <strong>my content</strong> with
    <em>markup</em>; my content wraps in the container,
    but that's not a problem for this technique.
</div>

That comes out with the content nicely displayed centered both horizontally and vertically. Unlike the line-height trick, it works well with content that wraps. Here's a live example.

I'm not saying we should have to say things are table-cells when they're not, but at least it's only in the presentation layer, not the markup.

Wednesday, 22 January 2014

Plain JavaScript

"I want to do this with pure JavaScript, not jQuery."
"How do I do this with raw JavaScript?"
"I don't like to use libraries, I like to use vanilla JavaScript."

Enough! Using a library like jQuery (or Closure or MooTools or...) is using plain/vanilla/raw/pure JavaScript. (I mean, unless you're using CoffeeScript, TypeScript, Dart, or other such languages.) Using a library doesn't change the language you're using.

jQuery is a library, not a language. You don't write things "in" jQuery. You write things with jQuery, in JavaScript. Or with MooTools, in CoffeeScript. Or with Closure, in Dart. You get the idea.

If you want to do things in the browser without a browser library, what you're doing is using the DOM API directly. Which is to say, writing with the DOM API, in (choose your language, probably JavaScript).

This isn't just a rant about prepositions. (It is a rant, but not just a rant.) It's about understanding what you're using, and how.

Saturday, 11 January 2014

Windows Pop-Up Shutdown Script

Sometimes, you want to configure a Windows system so that it shows a message when shutting down. In my case, it's to remind me (because I'm too bone-headed to remember!) to unmount any shares on the relevant box, since otherwise it tends to make the machine where I have them mounted...unhappy.

It's easy to do: First, create the script you want to run, perhaps:

WScript.Echo("Remember to unmount the shares!");

...in a file somewhere (shutdownmsg.js, in my case).

Now, we tell Windows to run that on shutdown:

  1. Run gpedit.msc
  2. Navigate to Local Computer Policy > Computer Configuration > Administrative Templates > System > Scripts
  3. Change the setting for Run shutdown scripts visible to Enabled (if you don't do this, your machine will basically hang on shutdown, waiting for you to reply to a dialog box you can't see)
  4. Navigate to Local Computer Policy > Computer Configuration > Windows Settings > Scripts (Startup / Shutdown)
  5. Double-click Shutdown
  6. Click Add and add your script (in my case, c:\bin\shutdownmsg.js)
  7. Close gpedit

That's it! Now when you shut down, the dialog box will appear and stop the entire process until you dismiss it. Conveniently for me, this happens before the network stuff is shut down, so I can unmount my drives cleanly.

Tuesday, 24 September 2013

Submitting forms programmatically

Very short one today, folks, because something surprised me: If you use jQuery to submit a form programmatically, it will trigger submit handlers on the form. This surprised me because if you use the DOM to do it, it doesn't. Consider this HTML:

<form id="theForm" action="http://blog.niftysnippets.org" target="_blank" method="GET">
    <p>This one will trigger the submit handler:</p>
    <input type="submit" value="Click to send form normally">
</form>
<p>So will this one (which surprised me):</p>
<input type="button" id="jQuerySubmit" value="Click to use jQuery to send the form">
<p>This one won't (standard DOM behavior):</p>
<input type="button" id="domSubmit" value="Click to use HTMLFormElement#submit to send the form">

And this code (below the above in the HTML file):

$('#theForm').submit(function() {
  alert("jQuery submit handler called");
})[0].onsubmit = function() {
  alert("DOM submit handler called");
};

$("#jQuerySubmit").click(function() {
  $("#theForm").submit();
});

$("#domSubmit").click(function() {
  $("#theForm")[0].submit();
});

Clicking the jQuerySubmit button triggers the handlers, clicking the domSubmit button does not. Live copy

So if, like me, you're used to that handler not getting called when you submit programmatically, well, jQuery changes that! If you want the old behavior, use the DOM directly (it's only three more characters :-) ).

Happy coding!