3

Q: the following image won't resolve as referenced from a .js script (the js script in is ~/scripts with all the other .js scripts out of the mvc3 box)

src: '/content/themes/base/images/down.gif'

This works locally but will not when deployed remotely to an iis6.0 server as a virtual directory.

I have tried:

src: '~/content/themes/base/images/down.gif'

but this wont' resolve locally or remotely.

The following will work remotely if I hard code the virtual directory path (virtualdirectoryname is the actual virtual directory)

src: 'virtualdirectoryname/content/themes/base/images/down.gif'
3
  • What folder is your js file in? You should just reference this with relative paths so it works in all scenarios. Oct 11, 2011 at 20:51
  • script is with all other scxripts: mvc3app\Scripts\ea.droplinemenu.js
    – genxgeek
    Oct 11, 2011 at 21:17
  • What are the actual full URLs to your web root locally and remotely? Where is the difference? Oct 19, 2011 at 15:30

2 Answers 2

0

You can't use ~ in javascript. Try:

../Content/themes/base/images/down.gif
5
  • Does browsing directly to the image work? Lets eliminate permission issues.
    – Valamas
    Oct 11, 2011 at 21:16
  • Yes, the image resolves in the browser per the path, /mvc3app/content/themes/base/images/down.gif.
    – genxgeek
    Oct 11, 2011 at 21:32
  • You mention a virtual directory. Is it setup as an application in IIS?
    – Valamas
    Oct 11, 2011 at 21:34
  • Yes, I have it setup as an application
    – genxgeek
    Oct 11, 2011 at 21:35
  • Sorry, I am out of ideas. Hopefully someone else can help.
    – Valamas
    Oct 12, 2011 at 21:49
0

You can handle this a couple of different ways. It looks like your best bet would be to create a Javascript function that gives you the base URL, then use that to build your paths.

Here's an example of this from http://www.gotknowhow.com/articles/how-to-get-the-base-url-with-javascript:

function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));


    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}

Then your code would look like this:

src: getBaseURL() + '/content/themes/base/images/down.gif'
2
  • That gives me my base url, hostip and not hostip/virtualdir. But what I need is my working directory. virtualdir that maps to a working directory that houses the content.
    – genxgeek
    Oct 12, 2011 at 22:26
  • Just modify it to pull back up to the 2nd / then. Oct 17, 2011 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.