Click here to Skip to main content
15,888,088 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
In some part of my code I have this

while(document.images[local].src != imgV)


So, I refer to the images this way:

vermelhaColocada.src = "imagens/something.gif";


But for some reason, document.images.src refers to the absolute URL path (http://localhost/..../images/something.gif).

This is a problem for me because I have to compare with a variable assigned manually:

var imgV = "http://localhost/..../imagens/something-2.gif";


So if I'm not running localhost (or anything I define on that variable), the web app will not work properly (i have to change everytime I execute the app remotely or something).

So I suppose the solution would be getting the URL absolute path, assign to a variable, and then join with "images/something.gif-2".

So my question is, how can I do that? There's any other solution?
Posted

1 solution

This is what I use on my web page:

JavaScript
function GetSiteRoot()
{
    var rootPath = window.location.protocol + "//" + window.location.host + "/";
    if (window.location.hostname == "localhost")
    {
        var path = window.location.pathname;
        if (path.indexOf("/") == 0)
        {
            path = path.substring(1);
        }
        path = path.split("/", 1);
        if (path != "")
        {
            rootPath = rootPath + path + "/";
        }
    }
    return rootPath;
}


I simply add the desired path/filename to what's returned, and I have a fully resolved URL.
 
Share this answer
 
Comments
Maxdd 7 6-May-11 8:10am    
It worked perfect, thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900