tag:blogger.com,1999:blog-5423986242878765882024-03-16T01:09:19.022+00:00Nifty SnippetsNifty Snippets<sup>TM</sup> of code, techniques, and information focussing on Ajax, web scripting, and engineering.T.J. Crowderhttp://www.blogger.com/profile/05828354736681391962noreply@blogger.comBlogger96125tag:blogger.com,1999:blog-542398624287876588.post-51149706805865859402018-06-15T18:41:00.001+01:002020-10-17T10:41:26.457+01:00Common `fetch` errorsThe new fetch API is a great improvement on XMLHttpRequest but it does have a very surprising behavior that catches people out a lot. Separately, because it uses promises, a lot of people using it fall into a common promise-related error as well. This post is a quick note about both of those.
Let's say we have an endpoint that returns JSON. This is the common code you see for consuming that:
T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com2tag:blogger.com,1999:blog-542398624287876588.post-49101805245317874862015-09-16T13:17:00.002+01:002015-09-16T13:17:45.164+01:00Automatic Semicolon InsertionI've been recommending to people that they not rely on Automatic Semicolon Insertion for years now, and frankly wish that the strict variant had removed it. Just wanted to share the best argument for putting semicolons where they belong that I've seen in a long time:var foo = {
a: "ay",
b: "bee"
}
(function() {
console.log("Hi there");
})();That blows up, because the opening ( in theT.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com4tag:blogger.com,1999:blog-542398624287876588.post-7681211502770952822014-07-29T23:41:00.001+01:002014-10-09T09:02:12.546+01:00JavaScript's Date/Time Format Is Not ISO-8601TL;DR JavaScript's date/time string format isn't ISO-8601. It's much simpler, covers fewer use cases, and assumes strings written without a timezone indicator are in UTC (whereas ISO-8601 assumes they're "local" time).Update October 2014: The draft ES6 specification has changed the timezone thing so ES6 will match ISO-8601 and assume local time, and some implementations (such as V8 in Chrome) T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com5tag:blogger.com,1999:blog-542398624287876588.post-72909901150577142012014-06-01T14:35:00.000+01:002014-06-01T14:35:34.513+01:00VirtualBox -- USB not workingJust 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 T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com1tag:blogger.com,1999:blog-542398624287876588.post-84934737942768751792014-05-29T12:24:00.000+01:002014-05-31T09:11:56.084+01:00Chrome and JavaIf 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 T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com5tag:blogger.com,1999:blog-542398624287876588.post-34634527302807262022014-03-17T08:31:00.001+00:002014-03-17T08:31:45.066+00:00Session tokens, cookies, and local storageLink-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.T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com2tag:blogger.com,1999:blog-542398624287876588.post-69427888125049647682014-02-18T13:10:00.000+00:002014-02-18T13:10:27.989+00:00Centering Made EasyAlmost 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;
T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com8tag:blogger.com,1999:blog-542398624287876588.post-60461369793190205932014-01-22T07:11:00.000+00:002014-01-23T07:31:36.852+00:00Plain 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 T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com9tag:blogger.com,1999:blog-542398624287876588.post-39208043622285297352014-01-11T12:45:00.001+00:002014-01-11T12:46:49.873+00:00Windows Pop-Up Shutdown ScriptSometimes, 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 T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com0tag:blogger.com,1999:blog-542398624287876588.post-87144599141049926082013-09-24T18:11:00.000+01:002013-09-24T18:12:51.693+01:00Submitting forms programmaticallyVery 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>
T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com0tag:blogger.com,1999:blog-542398624287876588.post-55050745250966831852013-05-15T17:27:00.000+01:002016-08-14T09:46:53.411+01:00Private properties in ES6 -- and ES3, and ES5Please note: ES6 changed markedly after this article was written in 2013; what was finalized and released as ES2015 in 2015 does not have private properties after all. The concept of the "private Name" object morphed into Symbol which was similar, but different, and in particular the idea that they would be non-discoverable was dropped. (Symbols are entirely discoverable, for instance with T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com5tag:blogger.com,1999:blog-542398624287876588.post-61634490824413610042013-02-13T17:40:00.000+00:002013-02-13T17:40:07.917+00:00Opera switching to WebKit+V8Opera has decided to switch to using WebKit and V8 for for all new products. First they'll start with a product for smartphones (since as they say, many mobile-facing sites are only/best tested on WebKit anyway), and then Opera Desktop and other products will follow.By my count, that leaves us with three major rendering engines (WebKit, Gecko, and Trident), and three major JavaScript engines (V8,T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com0tag:blogger.com,1999:blog-542398624287876588.post-62945576679350704722012-12-29T12:20:00.000+00:002012-12-29T12:21:23.186+00:00Amazon S3 Adds Root Domain SupportMore interesting news from Amazon: Now S3 supports static website hosting on the root of your domain. So in addition to http://www.example.com you can now have http://example.com. This comes warm on the heels of their having added CORS support in August and redirections in October. The directon is fairly clear, eh?T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com0tag:blogger.com,1999:blog-542398624287876588.post-78890575089373975122012-12-18T13:43:00.000+00:002012-12-18T13:47:52.548+00:00Website Feedback PopupsYou want my feedback on your website? Here's my feedback: Website "feedback" popups are irritating and intrusive.T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com1tag:blogger.com,1999:blog-542398624287876588.post-18818923013525086772012-11-09T09:49:00.000+00:002012-11-09T09:50:34.477+00:00Font Size UnitsI've always used points (pt values) for sizing fonts on web pages, e.g.:body {
font-size: 12pt;
}Those of you who know me know that I'm a developer, not a designer (in the web designer sense; I design systems, but that's different). I couldn't visual-design my way out of a paper bag. (Okay, maybe that's a bit harsh.) I don't think I'm a one-trick pony, but I'm definitely much more left- than T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com2tag:blogger.com,1999:blog-542398624287876588.post-30761028031772436252012-10-31T15:44:00.000+00:002012-11-01T08:22:27.750+00:00Blitz.io Updates Pricing ModelEarly this month I was checking out blitz.io and although it looked like a cool tool (and fun), the pricing model stopped me looking at it too closely, for reasons I explained to them:Hi folks,
Was just checking out blitz.io, which looks really cool (and fun), but I
was stopped pretty early on by the pricing. I don't understand why
the length of a rush is tied to the number of concurrent usersT.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com1tag:blogger.com,1999:blog-542398624287876588.post-41133485486992348822012-10-24T05:32:00.000+01:002012-10-24T05:41:03.093+01:00AsynchronicityI see questions like this one a fair bit: The author has written this code (and is apparently using jQuery):function Obj() {
this.id = 0;
this.name = '';
}
Obj.prototype.setName = function(name) {
this.name = name;
};
function init() {
var object1;
object1 = new Obj();
object1.setName("Chris");
alert(object1.name); // alerts 'Chris'
$.post('my_json_list.php', T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com1tag:blogger.com,1999:blog-542398624287876588.post-74323863175239651672012-10-04T15:20:00.000+01:002012-10-04T15:24:15.842+01:00I Don't Want To Create An AccountNote to online vendors:I don't want to have to create an account to give you business.Got it? By all means offer me the option of doing so, if that makes you happy, but if I'm buying 10 quid of lightbulbs from your site, or a one-off rail ticket, I really don't want to create a username and password, opt out of your effing mailing list, etc., etc. Imagine if you had to "create an account" with T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com2tag:blogger.com,1999:blog-542398624287876588.post-15032744356537545982012-10-03T23:29:00.001+01:002012-10-03T23:29:45.434+01:00You can contribute to caniuse.comI'm sure most of us have referred to (and probably cited) caniuse.com at some point. It's probably the most comprehensive collection of browser feature support around. Want to know the status of CORS support? Here you go.Well, now if you spot an error or want to add further information, you can do so fairly directly: caniuse.com is now on GitHub.I should probably note that caniuse.com is T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com0tag:blogger.com,1999:blog-542398624287876588.post-43152675208152195042012-09-27T11:51:00.000+01:002012-09-27T11:51:08.513+01:00Quick note on RegExp#lastIndexJust a quick note on RegExp#lastIndex: It's misnamed. It's not the "last" index of anything, it's the index of the next character in the string that will be looked at by the regex instance's exec function (if the regex has the global flag and exec is used on a string that's long enough). It's 0 on freshly-created instances. Just useful to remember, if you ever need to set it explicitly (which T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com0tag:blogger.com,1999:blog-542398624287876588.post-55949101206497761032012-09-26T11:41:00.000+01:002012-09-26T11:41:47.065+01:00Incredible. Extraordinary. Inspiring. Beautiful.Ten years of Hubble photographs of a tiny portion of the night sky, well away from the glare of the Milky Way, deep into the infrared and then corrected back to visible, the eXtreme Deep Field is nothing less than a breathtaking view deep into the Universe. Just...impossibly incredible.T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com1tag:blogger.com,1999:blog-542398624287876588.post-1563964094206628682012-09-01T17:08:00.000+01:002012-09-01T17:08:08.318+01:00S3 Adds CORS SupportSome interesting news today: Amazon's S3 now supports CORS, the Cross-Origin Resource Sharing standard. This means that in addition to storing your images, stylesheets, and JavaScript files in S3 as you might be now, you can now also store any static data or templates you want to retrieve via ajax there, assuming your user is using a browser that supports CORS (all modern ones do, one way or T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com0tag:blogger.com,1999:blog-542398624287876588.post-85590971727096058392012-08-23T07:54:00.000+01:002012-08-23T08:11:23.646+01:00A reminder how Microsoft used to drive web innovationAs IE6 finally rides into the sunset,* Nicholas C. Zakas offers us a reminder of how, in a series of browser releases culminating in IE6, Microsoft introduced many of the key web innovations we use today such as innerHTML, access to all elements (not just forms and such), Ajax, modern events, and several others. This isn't in any way to discount what Netscape and others have done, but it's worth T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com0tag:blogger.com,1999:blog-542398624287876588.post-18821468173583661992012-08-14T11:30:00.000+01:002012-08-15T07:00:54.331+01:00Measuring Scrollbar SizeNormally we want to avoid doing this sort of thing, but sometimes you just end up having no other option: Recently I couldn't avoid doing some sizing logic in JavaScript rather than CSS and markup, and I had to know the size of the scrollbars on specific elements. I found this post by Alexandre Gomes (which in turn was based on a MooTools forum thread; those forums are gone now), which shows a T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com1tag:blogger.com,1999:blog-542398624287876588.post-43819380062548980242012-08-08T09:46:00.000+01:002012-08-08T09:50:17.162+01:00jQuery - Element cleanup updateFor those who saw my jQuery - Cleaning up when elements go away post yesterday, I've updated it showing how we can do this right now, today, without waiting for the enhancement (or if the enhancement is never accepted). Oh, and the enhancement went from six lines to three. Many thanks to Dave Methvin for showing how (in both cases). Enjoy!T.J. Crowderhttp://www.blogger.com/profile/03619792122821936638noreply@blogger.com0