March 2010
1 post
jquery replacement
as i begin to make mobile apps, it’s become painfully clear that i simply don’t have the performance i’m used to in a browser. while i write fast javascript, i am stuck using jquery for the time being. I’m not a jQuery fan, but i’m not out to get it either. it is what it is, and it works.
The problem is that it works slowly. I re-wrote the fade routine a few months...
December 2009
1 post
libraries
i had sworn off creating libraries, but i can’t stop myself. I will be releasing three new libraries in the next few weeks.
1. a sequal to my F functional programming library, this time with several Function prototypes.
2. a Haskell data.list array method [].prototype package. almost all the Haskell functions are done.
3. a 10kb version of Prototype, all the same String and Array methods...
October 2009
2 posts
on accessibility
use consistent html from page to page.
even if your page is hard to use, a user knowing they have to jump ahead 20 links each page visit saves them a ton of time. if each page is different, that predictability is lost. It’s like the difference between cooking in your own kitchen versus a strangers. don’t make them constantly “look” around.
sectionize everything.
the...
4 tags
35char jsonp fetcher for jQuery
//so you can paste into firebug
$=jQuery;
// verify script load (just for this demonstration)
window.mini=function(){alert('hello');}
//here's how to add a script:
$("head").append("<script src='http://danml.com/mini'>");
July 2009
1 post
4 tags
Temporary CSS
Given the text of a styleSheet (strCSS), it will apply the styles to the current document.
The next time it’s called, the old styleSheet is replaced by the new text.
In this fashion, you can quickly create conditional styles document-wide.
The function returns the text it was fed, so you can use it in a chain.
function useCSS(strCSS) {
var D = document, ns, nsx, h =...
June 2009
1 post
Sprint for JavaScript
usage:
“Inside a string id\n or \n “
(where id is (optional) token from table below, and n is the argument position to substitute.
"": String (default)
"$": String
"#": Number
"%": Integer
"!": Boolean
"_": Char from charCode
"@": Locale version
javascript code:
function sprint(str){var S=String,A=arguments,c=S.fromCharCode,T={
"": S,
"$": S,
"#": Number,...
May 2009
1 post
what's that function?
JavaScript lack the ability to reflect a method’s bound property name to the method’s function itself.
I figured out a simple way around this:
function whoAmI(fn, that){
for(var i in that){
if(that[i]===fn){
return i;
}
}
} // end whoAmI()
this.fred = function john(){
var myName = whoAmI(arguments.callee, this);
return myName;
}
fred()//==="fred"
this could...
March 2009
2 posts
6 tags
on open source and js
I came across this register article “warning” that not all javascript is open source. I guess he doesn’t understand that all javascript run as source, so it’s all open, technically if not legally. Javascript can run, but it can’t hide.
Stallman also says that browsers should let you change the code they run, and that you should be able “to let users detect...
php vs js
I find php interesting. It’s more terse and expansive than my favorite language; javascript. It reminds me of VB6 a lot; tons of built ins and keywords. subtle syntax differences continue to frustrate me. I am torn as to which one is a “better” language. While PHP gives you a lot of functions to use, javascript aims for minimalism. PHP seems to require more code to do the same...
February 2009
1 post
site update
added an index to the site.
i figure why not let people see more than a single page.
i will be working on more creative displays, but the info is helpful, so here it is:
danml - index
January 2009
2 posts
president obama let us use your webpages
The US government creates and maintains vast storehouses of information. Most of these are publicly available online.
Among the more useful:
census data
weather data
royalty and advertisement free mapping
Images can come from anywhere, but text cannot.
Thus the tiger mapping service has a REST API that can be used by/from any webpage.
Any webpage can use image data from anywhere,...
green code
Netbooks, cell phones and other battery-powered devices are quickly becoming a popular way to access the web. I wonder how different kinds code impact the battery usage of these gadgets. A recent experiment showed that web pages using Flash sucked more power than pages with html and javascript alone. It says that “ajax” uses almost as much as Flash. I would presume he is talking about...
December 2008
3 posts
Random thoughts
Scenario: You have 5000 people using your web site every day.
Question: whats cheaper for you: smart servers and dummy terminals, or a dumb server and smart clients?
Which one uses javascript more?
Put those 3.8Ghz web browsers to work for you, code javascript.
snippit library
now available here.
go ahead and bookmark, updates will be auto-applied in the future as they roll out.
4 tags
finding square roots
here is an interesting example function for javascript.
while not needed, it serves to demonstrate the procedure using javascript.
function sqrt(number){
var guess = 2, old=0;
for(var step=0;step<128;step++){
old = guess;
guess = (guess + (number/guess)) / 2;
if(old===guess){break;}
}
return guess;
}
November 2008
3 posts
4 tags
my blog
News:
Readership is up as I am starting to link in public to the blog now.
I figure why not tell people about it eh?
Perhaps if nothing else, it’s an interesting example of what we can do without a server using static HTML and JavaScript, same-domain policy be damned.
I’ve dumped a few extra articles in addition to plenty of opinions, current JavaScript news, and useful...
3 tags
hacker Alphabit
hackerAlpha = [“4”, “8”, “[“, “)”, “3”, “|=”, “6”, “/-/”, “1”, “_|”, “X”, “1”, “|v|”, “|\|”, “0”, “|*”, “(_,)”, “2”, “5”, “7”, “(_)”,...
6 tags
javascript factorial
can it get any shorter?
I don’t think so, but feel free to prove me wrong.
function factorial(n){return n?n--*factorial(n):1}
October 2008
3 posts
3 tags
google image search API
http://ajax.googleapis.com/ajax/services/search/images?v=1.0&callback=gcb&q=obama
3 tags
Copy to Clipboard
Copying text to clipboard with JavaScript Here is a simple way to place small bits of text upon the user’s clipboard. Normally restricted by browser security, the...
4 tags
Converting Objects to Arrays
three handy functions for raking objects: obVals : an array of the object’s values (variants) obKeys : an array of the object’s keys (strings) obMap : an array of the object’s keys and values: [key,value] (arrays) Code: function obVals(ob) {var r = [];var i = 0;for (var z in ob) {if (ob...
September 2008
1 post
2 tags
Random Pages
make your own random page bookmark with customized keywords. Code: (function(){ var kws=['jesus','mary','joseph']; function Rnd(w) { return parseInt(Math.random() * (w + 1));} var url='http://delicious.com/tag/'+kws[Rnd(kws.length)-1]+'?min=10&random=1'; location.href = url; }()) bookmarklet url: Code: ...
August 2008
27 posts
Decorating text in javascript
Give plain old text some jazz with the crapify function! function crapify(w) { function Rnd(w) { return parseInt(Math.random() * (w + 1)); } function rndcol() { var r = (parseInt(Math.random() * 90) + 1).toString(16); return r; } var r = w.split(""); function letterMaker(a) { var fs = 120 - (Rnd(40) - 20) + "%"; ...
Random Thought
a b c 1 2 3 4 5 6
Random Thought
breaking news obama elected president of the US:
Are functions passed by reference or value
All the documentation i can find (ex: msdn jscript documents ) says functions, like other objects, are assigned to variables by reference. Code: foo = new Function ('var u="*************";'); var bar = foo; // creates a ref or a copy ? foo = new Function ('var u="_____________";'); //clobber foo alert( [ foo.toString(), bar.toString() ].join("\n\n")) this alerts: Code: function...
New javascript coming!
In case you missed it, the near-future of javascript was decided recently. Basically what happened is that the big changes we’ve been hearing about for Ecma4 (the best know flavor of which is being developed under the name JavaScript2) have been scrapped.
Microsoft has strong-armed the committee into scaling back the ambitions of the language. The new version...
video review
Video: Best Practices in Javascript Library Design
worth watching: yes in a sentence:good for beginner and intermediate scriptwriters to consider. stars: 2/4 info quality: 3/5- editorializing detracts from valuable content audio quality: 4/5 - decent, but inaudible audience questions toward end video quality: 2/5 - dark. presented text examples are difficult to read comments: jQuery...
Code formatting and compression
dan’s code validation can compress and reformat compressed code.
here is the starting input: a function to cleanup strings to parse dates into objects.
function parseDate(s) {if (s.match(/^\s?\d{4}-\d{2}-\d{2}T\d{2}\:\d{2}:\d{2}Z\s?$/g)) {}var d = new Date;try {d.setTime(Date.parse(s));} catch (t) {var td = s.replace(/[-_\\.]/gm, "/").replace(/T!h+/, " ");d.setTime(Date.parse(td));}return d;} ...
how2: get active script .src
try this inside a script file: var ts= document.getElementsByTagName("script"); alert (ts [ ts.length - 1 ].src )
Array Zen
zen is javascript 1.6 array methods like map and filter. these are native method in firefox, but they can easily be recreated elsewhere using standard ecma script: here is a replacement for imho the two most useful ones: Code:
if (!Array.prototype.map) {// from http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:map
Array.prototype.map = function (fun) {var len =...
video review
Video: Douglas Crockford: “Quality”
Douglas Crockford: “Quality” @ Yahoo! Video
worth watching: definitely!
in a sentence: A Must-see for all application developers, especially web devs.
stars: 4/4
info quality: 5/5 -Crockford siphoning off experience
audio quality: 5/5
video quality: 5/5 -text info is nice and sharp. color is slightly dull and lighting is poor,...
video review
Video: Joseph Smarr: High-performance JavaScript
Joseph Smarr: “High-performance JavaScript: Why Everything You’ve Been Taught Is Wrong” @ Yahoo! Video
worth watching: yes
in a sentence: Video is a good watch for javascript application developers.
stars: 2/4
info quality: 3/5 - could be more succinct, but a few good takeaways.
audio quality: 3/5
video quality: 2/5 -...
blog building
Creating a modern blog from the ground up is nowhere near as difficult as it used to be. API’s abound to store your posts and catalog your content. You don’t even need your own server anymore, you can use a provider like bloggers. I chose to serve my own html base template that is populated dynamically from different sources using JSON APIs. This allows a maximum of control over the...
data url test
click here to open/test the data url virtual file.
shoot for the moon
the moon is a nice international standard.
aim to make javascript that’s reliable in other languages.
function scope example
Original:
function test(){
function test(){
return test;
}
return test;
}
example 1
test().toString();
// result:
function test() {
return test;
}
Example 2
test.toString();
// result:
function test() {
function test() {
return test;
}
return test;
}
Example 3
test()().toString();
// result:
function test() {
...
a different take on OOP
function Mammal(){
var t={}
t.name="mammal";
t.repro="birth";
t.getSelf=function(){ return this; }
return t;
}
function Cat(){
t=new Mammal()
t.name="cat";
t.dob=now();
t.meow=function(){alert("meow!"); }
return t;
}
function Dog(){
t=new Mammal()
t.name="dog";
t.dob=now();
t.bark=function(){alert("bark!"); }
return t;
}
function Freak(){
t={}.merge(new Cat )
t=t.merge(...
list all mime types
document.write("");
for (var i = 0; i < navigator.mimeTypes.length ; i++) {
document.writeln("", i, "",
"", navigator.mimeTypes[i].type, "",
"", navigator.mimeTypes[i].description, "",
"")
if (navigator.mimeTypes[i].suffixes != "")
document.write(navigator.mimeTypes[i].suffixes)
else
document.write(navigator.mimeTypes[i].suffixes + " * ");
document.write("");
if...
mock array comprehension
for(var o=([1,2,3,4,5,6,7,8,9,10]),z=(o.length-1), r=[]; r[z]=o[z] * 5 ; --z ) r[z]+=5
// ==[10,15,20,25,30,35,40,45,50,55]
tracking user behavoir without javascript
the following is a scheme to track user movement without javascript. it could be used to help application testing and interface evaluation. or it could be used to track clicks without the A tag’s PING functionality. client html Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html...
Random thoughts
i am VERY biased in support of JavaScript. It keeps poking it head into more and more places. plus, there’s a lot of spillover to other popular languages as well. foe example. you can cut and paste the example AWK user-defined example function on wikipedia, and it run as javascript w/o modification.
PHP is very close as well. the variable syntax is a lot like python’s. the loops are...
Encoding text to hide it from prying eyes.
function off(n) {
var r = n;
var o = [];
var mx = r.length;
var sc = String.fromCharCode;
var tos = 33333;
for (var i = 0; i < mx; i++) {
o[i] = sc(r.charCodeAt(i) + tos);
}
return o.join("");
}
off("dan davis") // == "芙芖芣艕芙芖芫芞芨"
JMOD's Date protos
output of calling each on a date object:
bytes :4
getDayName :Saturday
getMonthName :August
now :Sat Aug 16 2008 18:45:20 GMT-0500 (Central Daylight Time)
since :61
toDate :2008-08-16
toDateLong :Saturday, August 16, 2008
toISOString :2008-08-16T18:45:20
toJSONString :"2008-08-16T18:45:20"
toQuick :August 16, 2008 6:45:20 PM
toShort :2008-08-16 18:45:20
toSource2 :new...
Loading and Saving Files with browser javascript
Most of the ajax examples I’ve come across are complicated and use a two-step asynchronous process to fetch data. new users get lost in the status checking and callback writing, and simply want to load a text file like a csv log file, an m3u playlist, or a css stylesheet into a variable. browser javascript can use standard http methods like GET, PUT, and DELETE to create, modify, and...
Object.merge
Object.prototype.merge = function (ob) {
var o = this;
for (var z in ob) {
if (ob.hasOwnProperty(z)) {
o[z] = ob[z];
}
}
return o;
}