Monday, June 28, 2010

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

// Lets have uri:
http://localhost:8080/a/b/c/script.js

//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

http://localhost:8080/a/b/c/script2.js

and raw value in MSIE 7.0

http://localhost:8080/a/b/c/script.js/../script2.js


So here comes the another magic constant 4 in getAttribute("src",4) for MSIE ;-), but ? it does not work ?! (now tested in MSIE 8)
http://msdn.microsoft.com/en-us/library/ms536429(v=VS.85).aspx
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.

No comments:

Post a Comment