Issue #84  (script.text, Testing, JSON/DB, Productivity)02/26/15


Support this week's primary sponsor:

90 Days Free of Crazy Egg: Heat and Eye Tracking Software 90 Days Free of Crazy Egg: Heat and Eye Tracking Software
Crazy Egg is must-have software if you're serious about conversion. Harness heatmaps and visitor intelligence, without the high costs. Special offer: try it free for 90 days.

Just this past week, I discovered the text property that is available on any script element in an HTML page. I'm surprised I haven't come across this before, and maybe you're in the same boat.

Let's say we have the following HTML:
 
<script class="sc">
  Example text inside a script element.
</script>
  
<script class="sc" 
        src="//code.jquery.com/jquery-1.9.1.min.js"></script>
  
<script class="sc" 
        src="//code.jquery.com/jquery-1.9.1.min.js">
  <!-- comment test -->
  <p>Example text inside a script element.</p>
</script>

Here we have 3 script elements – two with a src attribute set. I've also added a class to each, so I can collect them all without worrying about grabbing other ones I don't want when creating the demo page.

Notice that 2 of the script elements have some content included, the last one also including an HTML comment and paragraph tags. The text property for script elements allows us to grab the contents of the script element, similar to how the textContent property works on regular elements. In his Definitive Guide, David Flanagan says: "This makes a script element an ideal place to embed arbitrary textual data for use by your application." I've heard of developers using hidden textarea elements for this sort of thing, but not a script element.

So our code to grab the text might look like this:
 
var scripts = document.querySelectorAll('.sc');

console.log(scripts[0].text);
console.log(scripts[1].text);
console.log(scripts[2].text);

And of course, I could store those values wherever I want, but in this case I'm just outputting them to the console. See this demo to test it out, and notice that in that demo, I'm also defining the text content for the 2nd script element, then outputting that after it's set. So it's not read-only.

The stuff I've read on this property, however, seem to be a little contradictory and confusing, and there's definitely a bug in Chrome. Here's a description of my findings:
  • Flanagan seems to imply that the text property is available only on script elements that don't have a src attribute set. My demo disproves that in the 3 main browsers (although maybe I'm just misreading what he says).
  • If you plan to use a script element to store data, Flanagan recommends that you set the type attribute to something like "text/x-custom-data", to tell the browser's JavaScript interpreter not to try to execute the script.
  • The spec says this property "must return a concatenation of the contents of all the Text nodes that are children of the script element (ignoring any other nodes such as comments or elements), in tree order", however, all 3 browsers return the paragraph element in my demo and Chrome also returns the comment.
  • Bugs aside, I believe this property is supported in all browsers, even back to old versions of IE.
Maybe I'm misunderstanding something about this property, but I'm going to do some further testing and then I'll file any bug reports with the browsers. If you have any further info, feel free to reply to this email.


Now on to this week's tools!
 

Testing and Debugging Tools


Sportcoat
"Gather feedback where it matters most. Create better websites by getting opinions in context: in the browser."

Web Font Load
"The easiest way to install all of the Google Web Fonts on OSX."

Screenbeacon
"Automated Visual Testing and Monitoring. Get notified when your web design breaks."

Cross Browser Testing
"Cross browser test your website in dozens of browsers with instant results. Over 1,000 combinations of browsers, OS, and plugins."

Konsole.js
"An on-screen debugger/logger and (optionally) default browser console replacement. Konsole was created to have a quick and simple way to debug a project you are working on, without the need to have a debugger take up precious space."

WhatUsersDo
"Videos of real people speaking their thoughts using your website, app or prototypes."

POSThere.io
"Debug all the POST requests. Not sure what's getting POSTed? Simply point your code or web-hook to a unique POSThere.io URL, then open it in your browser to see exactly what was POSTed."

BuiltWith
Punch in a URL and this service will tell you what technologies the website is built on.


 
Support Web Tools Weekly:   384 pages of CSS for $7
 

Documents, Content, Files, etc.


Kirby
"A file‑based CMS. Easy to setup. Easy to use. Flexible as hell."

Tinychart
"Simple, ready-to-go charts." Just punch in the data in tabular or JSON format, and then download a PNG of the data visualized.

Ozh' Tweet Archiver
"Import and archive your tweets with WordPress."

Send To My Cloud
"Make a web page that lets people upload pictures, videos, PDFs, documents, proposals (and anything else you can think of) directly to your Dropbox or Google Drive."

PrivBin
A private, encrypted messaging service that destroys the message after it's read, or after 7 days.

pandoc-opml
"Generate OPML files from Markdown."

Espion
"The most comprehensive and powerful web scraping solution yet invented. Cloud-based."

 

Productivity Tools


SiteStalker
"Supervises websites and notifies you when your desired content hits the web."

Paperwork
Open source note-taking and archiving alternative to Evernote, Microsoft OneNote, and Google Keep.

Daily 140
"Get a daily email with the most recent follows and favorites of five Twitter users of your choice."

Stack
"Task management software for devs and designers."

Trippeo
"Beautifully simple business travel and expense management."

Let.ter
"You need a new email app. One that's just for writing emails."

Usersnap
"Communicate with your clients and co-workers more efficiently with screenshots." Also allows annotations for providing feedback.

WordBoard Keyboard
iOS app. "Hate typing the same thing over and over, one key at a time? Personalize and pre-program your keyboard with WordBoard. Now you can control what the keys input."


 
Support Web Tools Weekly:   384 pages of CSS for $7
 

A Tweet for Thought

Should we look forward to using ES6 features in our JavaScript? Kristofer Joseph thinks so.

 

Suggestions / Corrections

Made something? Send links via Twitter @WebToolsWeekly (details here). No tutorials or articles, please. If you have any suggestions for improvement or corrections, feel free to reply to this email.

 

Before I Go...

Believe it or not, this week I finally tried out Spotify (I still own CDs, for crying out loud!). Hackers who are also Spotify users might enjoy this Spotify Playlist Downloader.


Thanks to all for subscribing and reading!

Keep tooling,
Louis
webtoolsweekly.com
@WebToolsWeekly
Copyright © Web Tools Weekly, All rights reserved.

Email Marketing Powered by MailChimp