Function: var baseURL = self.location.href; baseURL = baseURL.split("/").slice(0,3).join("/")+"/"; Code (markup): document.getElementById(id).src = "'+baseURL+'/style/images/b.gif"; Code (markup): The above wont write the base url '+baseURL+' EDIT: I got it, should have been double quotes "+baseURL+"