halO

programming, hackery, rants, science and of course Opera

Subscribe to RSS feed

Posts tagged with "javascript"

I'm so tired of slow ads

, , , ...

Many ads are added by just linking an external script to the page. I'm going to show you how to stop all scripts that are not originating from the domain you are currently visiting.

Copy the code below, save it to a file 'prevent_all_external_scripts.js' and add it to a folder. I call my folder 'userjs' and is placed in my documents.

window.opera.rx295723629 = new RegExp('^http:\/\/([a-zA-Z0-9\u0080-\u00ff-.]*)?'+location.hostname);
window.opera.addEventListener(
 'BeforeExternalScript',
 function (e) {
   var src = e.element.getAttribute('src');
   if(src.match(/^http:\/\//) && !src.match(window.opera.rx295723629)){
      opera.postError('Script outside '+location.hostname+' was found and blocked: '+src);
      e.preventDefault();
   }
 },
 false
);

Open Tools->Preferences->Content->Javascript Options and add the folder to the text box at the bottom, where it says "User Javascript Files".

Now any site you are visiting will load scripts originating from the current domain, but ignore external scripts.

Faster for-loops, update

, ,

In my blog entry about Faster for-loops i wrote about how to increase the efficiency of your for-loops. Virtuelvis pointed out an article at userjs.org with even faster for-loops (which was my original inspiration for the blog entry, but could not remember the source).

Example from userjs.org

var rows = document.getElementsByTagName('tr');
for( var i = 0, row; row = rows[ i]; i++ ) {
  row.className = 'newclass';
  row.style.color = 'red';
  ...
}

The code works by exploiting the fact that assignment of a variable returns the value the variable was assigned. Confusing you? Let's use an example. If you have a variable x and evaluate if(x = 5) it would be equivalent to evaluating if( 5 ). Since 5 evaluates to true in a boolean context, it would be equivalent to if( true ). Which boolean a value is evaluated as depends on the programming language, but most i know of consider 0 (zero), null and the empty string as false. The reason the above loop works perfectly is that as soon as the array goes out of bounds, it returns null and ends the loop before any harm is done.

So, we see that the loop assigns the current item to a variable and checks the assigned value in a boolean context for whether to continue or not. This works great if you are looping through a set of DOM elements for instance, but if the contents of the subject array is unknown you should be careful. Consider an array that contains the values 23, 56, 0 and 85. The loop would arrive at the third element and evaluate "row = 0", which evaluates to false in a boolean context and thus end there without reaching the fourth value.

The moral of the story is "If thou wishes to optimize thine code, know the contents of thy arrays" - or something like that...

Faster for-loops

, ,

I remember learning this from some document about user-javascripts found at opera.com, but haven't been able to relocate it. It's highly useful because it's so simple yet can save quite a bit processing power for large loops, so I figured I'd post it here.

The point is that most people, when writing a for-loop, will do like this:

for(var i=0;i<somearray.length;i++){
   // do something here
}

This is actually quite wasteful since the length of will be evaluated once for every item in the array. It can also cause bugs if one is not aware of this and adds or removes items from the array inside the loop.

A better way to do it is like this:

for(var i=0,end=somearray.length;i<end;i++){
   // do something here
}

Memorize it and do it like this as default. The times you actually need to reevaluate the array length every run, you'll remember it, so don't worry.

A (Re)-Introduction to Javascript

, , ,

I stumbled across a nice paper which summarizes nicely the capabilities of the core functions in java/ecma -script. If you're new, or anything other than an expert, it might provide a nice reference. Combined with Quirksmode.org for an introduction to the correct philosophy (object-detection etc.), the two should provide for a good learning curve for newbies.