views:

79

answers:

3

I'm using JQuery to switch out an image src thusly:

$("#myImg").attr("src", "../../new.gif");

notice the relative pathing on the new src. Unfortunately, this isn't portable when I deploy my app. In my MVC app I'm using a ResolveUrl() method that will fix the pathing problem for me so it's portable, but now my JQuery image src swapper doesn't work right since it now switches the correctly resolved path to a broken relative one.

<img id="myImg" src="<%=ResolveUrl("~/Images/transparent.gif")%>" />

What I want is for JQuery to just flip the actual filename and leave the path untouched. My first thought would be to

// pseudocode javascript jquery on my thought on how to approach this prob
var oldFullPath = $('#myImg").GetTheImgSrc;
var newFileNameWithPathIntact = someRegexAddNewFileNameWithOldPath
$("#myImg").attr("src", newFileNameWithPathIntact);

but that seems rather gross and un-JQuery to me. Anyone got a better way?

+2  A: 

You can use the resolveurl right in the javascript:

$("#myImg").attr("src", "<%=ResolveUrl("~/Images/new.gif")%>");

That of course assumes that you've included the javascript right in the view. If you have a requirement that this script must live in a separate script file from the html request, then you can just have a view which is a javascript file ... and just reference that URL in the script src:

<script language="javascript" src="<%= Url.Action("MyMethod") %>" />
Joel Martinez
I know in WebForms the designer doesn't treat this very well and might give you warnings but it is very valid, I use this all the time in my script to inject the nasty clientId's. +1
JoshBerke
While I don't have a req to have this js method in a separate file, It's called by several views so it's obviously better off in a single location. I'm not understanding your second option of having the view be a js file. Can you provide more detail please?
Kevin Won
Ok, what I meant was to make an action method, called "Script" (for example). Then in the "Script.aspx" view you can simply write javascript (along with dynamic elements as described above). Finally, by putting the URL to your action method in the src attribute of the script, the browser will request and execute your action method.
Joel Martinez
that's cool. It seems sort of heavyweight for what I want to accomplish but I like it as a general tactic.
Kevin Won
+4  A: 

you could use something like this:

var oldImage =$("#myImg").attr("src");
var imagePath = oldImage.slice(0, oldImage.lastIndexOf("/")) + "/new.gif";
$("#myImg").attr("src", imagePath ); 

EDIT: better code...:)

Mark Schultheiss
This is the best solution for my problem. Small prob with the code snippet: if you hack off the val() on the first line it will work. Apparently JQuery attr() will return a val if you only provide one arg
Kevin Won
Yep, my bad, typing too fast :) Editing for that.
Mark Schultheiss
+2  A: 

why not just use a variable for the root of the app that you can use for these types of situations.

var root = "<%=ResolveUrl("~/") %>";

Now you can easily construct your image path

$("#myImg").attr("src", root + "images/" + fileName);
hunter
problem is that it would be sort of spaghetti to get this set up right when I have the .js in a separate file.... every page using the .js file would need to set the root, wouldn't it? that seems a maintenance issue (though I like the elegance). or am I missing something?
Kevin Won
You could set the root in the master page as a global var.
Emmett
you can't set the root var in the master page because if you end up using the var in say your /customer/signup view the global var will be '/' where in /customer/signup/ it needs to be '../../'
Kevin Won
@Kevin - that's simply not true. Doing var = "<%=ResolveUrl("~/") %>" will alway render the root path regardless of how deep you are.
hunter
@Kevin - if you set the javascript root in the master page BEFORE any other .js files are included, the root variable will be a "global" variable for all inline or included javascript in your project and you will only need to declare it once.
hunter