1

I have some jQuery that writes some image tags to the browser, but I want to reference them such that I don't have to hardcode the path, and I'm not able to use relative paths either. This is a .NET application, and I am trying to use server side tags.

So, I changed the following:

$('#IMG_' + myID).attr('src','../images/plus.gif');

to:

$('#IMG_' + myID).attr('src','<%= Url.Content("~/images/plus.gif") %>');

or:

$('#IMG_' + myID).attr('src','<%= ResolveUrl("~/images/plus.gif") %>');

When it runs I get javascript errors, Invalid argument on line 48, char 325 of jquery-1.4.2.min.js.

4
  • 1
    Do a view source and post what's getting served to the client. Also, have you installed firebug or are you using IE 8's developer tools? They make diagnosing these kinds of things much easier. Commented Jul 6, 2010 at 0:45
  • also helps not to use minified versions of scripts whilst in dev\debug mode. You get better error info from non min js. Commented Jul 6, 2010 at 0:47
  • 2
    Silly question, but just checking: This code is on an aspx page and not a .js file, right? Commented Jul 6, 2010 at 0:57
  • Daniel, I think you found the root of my problem with that "silly question". When I move my code from the .js file to the .aspx page it didn't like the URL.Content(), but it did like ResolveURL(). Problem is, I need the jQuery to be in an external file. I guess I can't use Server Tags then correct? For now it seems to work if I prefix the project name to the URL: $('#IMG_' + myID).attr('src','/PROJNAME.Web/images/plus.gif'); Commented Jul 7, 2010 at 20:52

2 Answers 2

2

Here's something I've done in a couple projects. It's designed to be used with ASP.NET MVC, but I imagine it could be adopted to use with WebForms as well.

script: common.js

function Environment() { }    
Environment.vroot = '';

Environment.getUrl = function(path) {
    return Environment.vroot + path;
};

partial view/user control: CommonScript.ascx

<script type="text/javascript">
    Environment.vroot = '<%= Url.Content("~/") %>';
</script>

I just put CommonScript.ascx on my master page, but you can put it wherever you want. It needs to be rendered after the script reference to common.js though.

If you use this, you can just do this to set your image url:

$('#IMG_' + myID).attr('src', Environment.getUrl('images/plus.gif'));
Sign up to request clarification or add additional context in comments.

1 Comment

This works for me. this is the same pattern I am using on the ASP.NET side. I think I will use this for now on. Thx!
1

If you're not using virtual directories on any environment you could just use /images/plus.gif

If your site has an url like this localhost/mysite/default.aspx it won't work

1 Comment

I tried just /images/plus.gif, but that did not work. What does work is /PROJNAME.Web/images/plus.gif. Is there any time when the /PROJNAME.Web/ would cause an issue?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.