Rant on web browsers

C

Chris Angelico

Random rant and not very on-topic. Feel free to hit Delete and move on.

I've just spent a day coding in Javascript, and wishing browsers
supported Python instead (or as well). All I needed to do was take two
dates (as strings), figure out the difference in days, add that many
days to both dates, and put the results back into DOM Input objects
(form entry fields). Pretty simple, right? Javascript has a Date
class, it should be fine. But no. First, the date object can't be
outputted as a formatted string. The only way to output a date is "Feb
21 2011". So I have to get the three components (oh and the month is
0-11, not 1-12) and emit those. And Javascript doesn't have a simple
format function that would force the numbers to come out with leading
zeroes, so I don't bother with that.

What if I want to accept any delimiter in the date - slash, hyphen, or
dot? Can I just do a simple translate, turn all slashes and dots into
hyphens? Nope. Have to go regular expression if you want to change
more than the first instance of something. There's no nice string
parse function (like sscanf with "%d-%d-%d"), so I hope every browser
out there has a fast regex engine. When all you have is a half-ton
sledgehammer, everything looks like a really REALLY flat nail...

Plus, Javascript debugging is annoyingly difficult if you don't have
tools handy. I need third-party tools to do anything other than code
blind? Thanks.

Oh, and "int i" is illegal in Javascript. Whoops. That one is my fault, though.

Javascript's greatest strength is that it exists in everyone's
browser. That is simultaneously it's worst failing, because it becomes
nigh impossible to improve it. If Chrome's V8 starts supporting new
features and everyone else's JS engines don't, we can't use those
features. Even if they're added to the standard, there'll still be old
browsers that don't support things. The only way to add to the
language is to dump stuff into a .js file and include it everywhere.

But if anyone feels like writing an incompatible browser, please can
you add Python scripting?

Chris Angelico
 
C

Chris Angelico

On 14/06/2011 07:31, Chris Angelico wrote:


You might find that Pyjamas already fill your needs python/javascript wise.
It is truly great to just write python, translate it, and then have it work
in the browser.

I had a (very) quick glance at that; it entails a fairly large library
that has to get loaded. It still comes down to "the only way to
improve this is to dish out a huge helping of .js to everyone who
comes", which imho is unideal.

That said, though, my context for this job was "tiny thing, just do
the same as you're doing on the back-end". For something larger, I may
well give Pyjamas a whirl.

Chris Angelico
 
A

Asen Bozhilov

Chris said:
I've just spent a day coding in Javascript, and wishing browsers
supported Python instead (or as well). All I needed to do was take two
dates (as strings), figure out the difference in days, add that many
days to both dates, and put the results back into DOM Input objects
(form entry fields). Pretty simple, right? Javascript has a Date
class, it should be fine. But no. First, the date object can't be
outputted as a formatted string. The only way to output a date is "Feb
21 2011". So I have to get the three components (oh and the month is
0-11, not 1-12) and emit those. And Javascript doesn't have a simple
format function that would force the numbers to come out with leading
zeroes, so I don't bother with that.

Actually there is not Date class. There are not any classes in
ECMAScript.
What if I want to accept any delimiter in the date - slash, hyphen, or
dot? Can I just do a simple translate, turn all slashes and dots into
hyphens? Nope. Have to go regular expression if you want to change
more than the first instance of something. There's no nice string
parse function (like sscanf with "%d-%d-%d"), so I hope every browser
out there has a fast regex engine. When all you have is a half-ton
sledgehammer, everything looks like a really REALLY flat nail...

function formatDate(date) {
return ('000' + date.getFullYear()).slice(-4) + '-' +
('0' + (date.getMonth() + 1)).slice(-2) + '-' +
('0' + date.getDate()).slice(-2);
}

formatDate(new Date());
Plus, Javascript debugging is annoyingly difficult if you don't have
tools handy. I need third-party tools to do anything other than code
blind? Thanks.

It depends on the environment. It is good idea to read c.l.js and
JSMentors.
 
D

Daniel Kluev

Regarding pyjamas lib size, its actually not that big if you want only
bare python, without DOM wrapper.

You only need pyjslib, which is less than 30kb gzipped when compiled
even with --strict (quite verbose mode).
Even that could be further reduced if you drop unused by your code
python features from it.
 
T

Thomas 'PointedEars' Lahn

[I am biting only because this is my field of expertise, and I am really
getting tired reading from people not having a shadow of a trace of a
minimum clue what these languages that I like can and can't do.]

Chris said:
Random rant and not very on-topic. Feel free to hit Delete and move on.

Why not post on-topic in the first place, and *ask* before being
presumptuous? Get better.
I've just spent a day coding in Javascript,

There is no "Javascript".
and wishing browsers supported Python instead (or as well).

Some browsers do in a way, because of the ECMAScript implementations they
employ. Mozilla.org JavaScript 1.7+ is pretty pythonic as compared to, e.g.
Microsoft JScript. Array comprehension, Array methods like filter(), and
iterators/generators come to mind. This is not a coincidence; Brendan Eich
has explicitly referred to Python in his blog.
All I needed to do was take two dates (as strings), figure out the
difference in days, add that many days to both dates, and put the results
back into DOM Input objects (form entry fields).

How that would be done would first depend on what you consider a day to be.
(We have been over this elsewhere.) You can easily compute the difference
between the time value of Date instances, which is stored as milliseconds
since epoch, by subtraction (conversion to number is implicit, you do not
have to call the getTime() method):

var d1 = new Date(…, …, …, 12);
var d2 = new Date(…, …, …, 12);
var ms = d2 - d1;

[I have been told that the 12 hours into the day can avoid problems with DST
changes. You may use any hours value that you see fit, even none (which
defaults to 0).]

Dividing that by the number of milliseconds per "day" (which is conveniently
written 864e5) would give you a good idea of the number of "day"s (but watch
out for floating-point precision).

You can also write a method (of Date instances, if desired!) that can take
calendar days into account – like saying that there are two days "between"
`new Date(2011, 5, 22)' and `new Date(2011, 5, 24)' –, since overflow on
Date instances is easily detected:

Date.prototype.diff = function(d2) {
// …
};

var d = new Date(…);
var diffInDays = d.diff(new Date(…));

Finally, adding or subtracting calendar days to a date is very simple, of
course:

var d = new Date();
d.setDate(d.getDate() + 2);
Pretty simple, right? Javascript has a Date class,

No, it does not. (Have you, by chance, read Flanagan's latest edition on
the subject, full of misconceptions?)

For your purposes, "Javascript" has no classes at all; if accepted as an
umbrella term (which I strongly recommend against¹), then the programming
*languages* it describes in your context all use *prototype-based*
inheritance.
it should be fine. But no. First, the date object can't be
outputted as a formatted string.

Yes, it can. There are several methods on the Date prototype object for
that, and you can always write and add your own.
The only way to output a date is "Feb 21 2011".
Wrong.

So I have to get the three components (oh and the month is
0-11, not 1-12) and emit those.

Not necessarily. (Yes, the month value is zero-based. This is not
particular to ECMAScript implementations.)
And Javascript doesn't have a simple format function that would force the
numbers to come out with leading zeroes,

But it is easily written, simplified:

function leadingZero(n, width)
{
n = String(n);
var len = n.length;
if (len >= width) return n;

var a = [];
a.length = width - len + 1;
return a.join("0") + n;
}
so I don't bother with that.

Your problem alone.
What if I want to accept any delimiter in the date - slash, hyphen, or
dot?

The most simple way is to convert it so that the string format is understood
by the Date constructor. There is precedence for the formats accepted by
implementations in Web browsers as they are specified in ECMAScript Ed. 5.

However, the most reliable way, unless you are using dates before 100 CE, is
to parse the components and pass them as separate arguments to the Date
constructor. There is even a way to use an Array instance for that (a
construct method added to Function.prototype or Date), so you can reuse the
return value of String.prototype.match().
Can I just do a simple translate, turn all slashes and dots into
hyphens?

Yes, you can, but it depends on the input format.
Nope. Have to go regular expression if you want to change
more than the first instance of something.

Again, that depends on the input format.
There's no nice string parse function (like sscanf with "%d-%d-%d"), so I
hope every browser out there has a fast regex engine.

I think sscanf() can be easily written (and is going to be [once again];
still working on an efficient sprintf()).

But why accept the date as a string, which is known to be ambiguous, in the
first place? Date input should be facilitated by three form controls, not
one. The least you should do if you use only one control is to provide a
date picker widget (HTML5 defines a way to do this without scripting, but so
far the feature appears to be experimental at least in non-mobile browsers).
When all you have is a half-ton sledgehammer, everything looks like a
really REALLY flat nail...

When all you have is a toothbrush, every dirty room must look like taking a
lifetime to get cleaned up… RTFM!
Plus, Javascript debugging is annoyingly difficult if you don't have
tools handy.

Script debuggers are built into most recent Web browsers. AFAIK of the Big
6[tm] (IE, Fx, Op, Cr, Sf, Kq) it is only Firefox which requires e.g.
Firebug to be installed as an extension. But installation of that is very
straightforward, and useful.
I need third-party tools to do anything other than code blind?

No, you don't. WebCore-based browsers et al. have debuggers built-in
(Ctrl+Shift+J etc.) All widely distributed Web browsers have an error
console built-in, and almost all graphical Web browsers to date support the
`javascript:' URI scheme, with which you can test script code from the
Location/Address Bar even without an error console (just be sure that you
use the `void' operator or other means that return an undefined-compatible
result).

You're welcome.
Oh, and "int i" is illegal in Javascript.

That depends on your idea of "Javascript". It is certainly a syntax error
in implementations of ECMAScript Ed. other than 4.
Whoops. That one is my fault, though.

Yes, those languages are loosely typed. Like Python, BTW.
Javascript's greatest strength is that it exists in everyone's
browser.

A language named "Javascript" exists in exactly no browser.¹
That is simultaneously it's worst failing, because it becomes
nigh impossible to improve it.

No, it has been done.
If Chrome's V8 starts supporting new features and everyone else's JS
engines don't, we can't use those features.

Yes, we can. You only cannot because you are too lazy.
Even if they're added to the standard, there'll still be old
browsers that don't support things.

You can deal with incompatible syntax extensions the same as before with,
e.g. Netscape 4. eval() comes in handy there, but you lose performance.

You can deal with API extensions the same as before, by run-time feature-
testing (which costs performance as well, but when cleverly done not nearly
as much).

Obviously you have not the slightest idea what you are talking about. But
that is a common trait among JavaScript/ECMAScript neophytes.
The only way to add to the language is to dump stuff into a .js file and
include it everywhere.

No, you can make adjustments on an as-needed basis (much like in Python).
But if anyone feels like writing an incompatible browser, please can
you add Python scripting?

Get yourself informed for a change!

______________
[1] <http://PointedEars.de/es-matrix>
 
L

lkcl

But if anyone feels like writing an incompatible browser, please can
you add Python scripting?

http://wiki.python.org/moin/WebBrowserProgramming

already been done, chris - you want the firefox plugin, pyxpcomext
and then if you actually want to manipulate the DOM in the browser
engine as well, you need the 2nd plugin xpcom as well. oh, i also
forgot (because it's quite new) there's firebreath as well:
https://github.com/Nitrogenycs/firebreath-x

so... yeah, if you're completely off your head, knock yourself out :)

l.

p.s. pyjamas rocks! sorry, had to say that.
 
L

lkcl

Random rant and not very on-topic. Feel free to hit Delete and move on.

I've just spent a day coding in Javascript, and wishing browsers
supported Python instead (or as well). All I needed to do was take two

ok your next best thing is to try pyjampiler. it's the absolute
minimum and easiest stuff you'll need to be able to write code in
python yet integrate it easily into a pre-existing web site. there
are alternatives out there (competitor projects to pyjamas) - take a
look on http://wiki.python.org/moin/WebBrowserProgramming

l.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top