• Changing an Image src in Opera

    Historical artifact (2026): This post describes a bug in Opera 12 on the Presto engine, which Opera replaced with Chromium in 2013. The problem no longer exists in any modern browser. The article is preserved as a reminder of the times when cross-browser development was… more interesting.

    What could be simpler than changing an image on a page with JavaScript? It seemed to be the real 2010s outside; various cross-browser libraries like jQuery had long been in use, accounting for every tiny whim of every engine. What problems could possibly appear? As it turned out, all sorts. The obvious attempt to simply change the img src did exactly nothing.

    $('#kcaptcha').attr('src', '../includes/captcha/run.php');

    Apparently, the browser believed that changing meant replacing one thing with another, and that writing the same thing twice made no sense. There is some logic in that, of course, but perhaps programs should not argue with programmers. Fine, I then tried adding some changing part to the path, one that would not affect the image output from the script but would force the browser to refresh it.

    $('#kcaptcha').attr('src', '../includes/captcha/run.php#' + Math.random());

    Everyone except Opera fell for that trick: they honestly reloaded the poor image, as if these were completely different paths rather than a fragment added to the URL. But the Norwegian beauty persisted in her unwillingness to compromise. And to make friends with her, I had to make certain adjustments, removing the random number from the fragment and putting it into GET:

    $('#kcaptcha').attr('src', '../includes/captcha/run.php?junk=' + Math.random());

    At this point Opera softened and obediently started changing the image.