Monday, June 28, 2010

SCRIPT.src vs. getAttribute("src") and magic 4 for MSIE

// Lets have uri:

//1. popular (incorrect) way to find URI for current script (inside the script code)
var script_src = (scripts = head[0].getElementsByTagName("SCRIPT"))[scripts.length - 1].src;

//2. popular trick to "resolve" uri to another script in the same "directory"
// src becomes:
// http://localhost:8080/a/b/c/script.js/../script2.js
// which is then transformed by browsers in actual http request to
// http://localhost:8080/a/b/c/script2.js

however when combined together, it can turn deadly in Cross-Browser code
since .src returns "dots removed" in FF


and raw value in MSIE 7.0


So here comes the another magic constant 4 in getAttribute("src",4) for MSIE ;-), but ? it does not work ?! (now tested in MSIE 8)
so you have to use .src !

As far as I know even jQuery does not have fix for this,
even if they have for getAttribute("href",2)

Edited in 2011:
Confused ? me as well. I have to retest all this because MSIE 8 seems to somehow work with .src and not work with getAttribute("src,4").
Work means returning "expanded" and "normalized" uri.

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or learn thru Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai