Skip navigation.

miscoded

the web is a hack

Posts tagged with "javascript"

useful JS snippets

, ,

To see all direct properties and methods of an object (does not recurse):

function objinspect(obj){var s='';for(var e in obj)s+=e+':'+obj[e]+'\n';opera.postError(s);}


Call it with the object you want to look at, for example objinspect(window); .

To emulate the document.load method in Opera:
if(!Document.prototype.load){
    Document.prototype.load=function(uri){
        if(this.documentElement){this.removeChild(this.documentElement);}
        var doc=(document.implementation.createLSParser( document.implementation.MODE_SYNCHRONOUS, null )).parseURI(uri);
        this.appendChild(this.importNode(doc.documentElement, true));
    }
}


BTW, why does Mozilla's document.load documentation claim that this method is from the DOM3 Load and Save spec when it isn't mentioned there?

To see all code that will be passed to eval() before evaluating it:

window.eval=(function(e){return function(s){opera.postError(s);return e(s);}})(window.eval);

how to shoot yourself in the menu with a variable

, , , ...

Opera uses the Browser JavaScript feature to automatically fix older scripts that otherwise would not run correctly.

One of the interesting challenges of doing that is to work out how, exactly, browser.js can tell when Opera is about to run an "outdated" script that needs fixing? Because if we apply the fix to a modern script, we might break things instead of fixing them..

Until today I thought the AllWebMenus fix was one of the safest in the book. It takes advantage of the fact that every AWM script contains a "build number" variable showing what version the script is. This variable is not even part of the comments where webmasters might remove it, it is right there embedded in the script. It is also relatively unlikely (though not impossible) that other scripts would define a variable called awmBN with a value that would trigger the patch, and even more unlikely that the patch would break a script doing so.

OK, think again. Browser.js breaks the menu on Aquaria.info because the inline script says

var MenuLinkedBy='AllWebMenus [2]', awmBN='438';

while the script file the page links to actually reveals a much more recent build number:

//****** AllWebMenus Libraries Version # 626 ******

So the webmaster has changed the .js files to upgrade, but not bothered changing the script in the HTML file. Not such a safe patch after all..

Once I stop saying "d'oh" I'll have to figure out what to do about it.. :-(

dead live

, , , ...

live.com was fixed to work in Opera but apparently recent changes have broken it again. Here is why:

  • They declare a namespace without associating a namespace URI with it: <html xmlns:Web> in a normal text/html document
  • When they use the namespace in <web:binding>, Opera will not return that element when you do document.getElementsByTagName('binding')
  • (Also note that they use a capital letter in the namespace declaration and lower case in the document)

Now, this would work fine if they sent an XHTML content-type, added a namespace URI and fixed the case issue.

But is it a bug in Opera? I haven't been able to interpret the xmlns spec in enough detail to understand whether this is covered. Or is it in the DOM spec? Validation certainly seems to say it is wrong to declare a namespace without URI.

The only thing I know for sure is that FireFox does the same as Opera, and that might indicate that a standard is being followed. Perhaps readers more skilled at xmlns juggling can enlighten me, if not I'll learn when developers comment on the bug report..

Ryanair, meet Æ, Ø and Å

, ,

Ryanair, if you want customers to fly with you it may be a good idea to avoid rejecting them arbitrarily because they have names with characters outside US-ASCII. Seriously.

Web 2.0: same sniff, new wrapping?

, , , ...

Eskobo uses the Microsoft Atlas framework. Atlas is pretty nifty actually, I especially appreciate it for being readable and un-obfuscated so it's simple for me to debug, unlike several other . unreadable . nightmares out there..

The problem at Eskobo is that you can not "minimize" the boxes with the little triangle on the left. Here's a screenshot demonstrating how I removed a single anti-Opera statement to enable that feature. Works like a charm once you disable their pointless "do not do this in Opera" crap. In this supposedly enlighted age of Web 2.0 and unobtrusive JavaScripts and AJAX enhancements.. Why do I still have to waste my time on ridiculous stuff like this? :mad:



Get a clue, guys.

To try this at home: load Eskobo, view source, search for and remove
if(cart_browser_opera){return false;};

Save and/or re-load from cache (as in using the menu entry Tools > Advanced > Re-load from cache in 8.5 or the "Reload from cache" button in 9's editor).

Whops, I wrote another Opera showcase..

, , , ...

Web developers like to complain about browser x or browser y being "difficult" to work with. Bah. Sure, there are really annoying bugs in any browser, but most of the time the one you test initially in and are used to will be easy. The others will present small or big surprises. A couple of times I have written pages that only worked in Opera, because I happened to use both standardised features that are missing from Internet Explorer and IE-specific features supported by Opera but not others. A natural risk when one knows a specific browser's capabilities very well.

Here's a page I created recently that I thought was pretty standards-compliant (apart from event.offsetX and offsetY - perhaps I should ask WHAT WG to standardise those?):

Japanese new year's breakfast

..but whops, I did it again: it only appears to work in Opera. All the content is visible in any browser and I simply like creating Opera DOM capability showcases, so I won't add workarounds - but it makes me rather curious that even old Opera 7.23 does a perfect job but new FireFox 1.5 fails. I know IE doesn't get the setAttribute('style', 'some css code') part, but I have no idea where FireFox stumbles.

IE's scrollHeight logic

, , , ...

Element dimension properties is such an unstandardised mess. Do not ask me to be your guide through this maze, but below is at least some documentation of how scrollHeight behaves in IE.

If element does not have "height" specified, scrollHeight returns height of element contents + padding + border (in other words exactly what offsetHeight does).

If height is specified scrollHeight returns height of element contents + padding only.


As for Opera, we seem to have some bugs. The problem with getting it right is that the scrollHeight beast is not in any standard, so we just have to try to do "what IE does" and discover through users reporting problems out there that we actually missed out half of the logic and ended up doing something entirely different and incompatible.

This is why standards are good for you.

What Opera does: scrollHeight returns border-top-width + padding-top + height of element contents (!). How did we get there? Including only half of the border and padding width - top but not bottom values?? I don't have a clue.

spot my browser.js bug

, ,

Usually I use this blog for opining on the quality of other people's and systems' code. Well, that's a bit one-way and biased, isn't it? Since I just found a bug in browser.js, written and maintained by yours truly, I'll sweep the street in front of my own door (as a Norwegian saying goes).

So, can you spot the broken logic in this browser.js excerpt?

if( hostname.indexOf('walla.co.il') ==hostname.length-11 ){
// Walla.co.il odd CSS styling causes display problems
// Bugs 184398, 184399
opera.addEventListener('BeforeEvent.load',
function(){ if(document.documentElement){(document.documentElement.appendChild(document.createElement('style'))).appendChild(document.createTextNode(' .btn-t,.btn{display:inline !important;} .wp-0-b{width:auto !important}'));
opera.removeEventListener('BeforeEvent.load', arguments.callee, false); 
opera.postError('Opera has modified the JavaScript on '+hostname+'. See browser.js for details');}}
,false);
}