views:

1051

answers:

3

So I have this...

$(this).attr("href", "http://site.com/images/downloads/wp-" + $(this).parent().parent().attr("id") + "-1024x768.jpg");

The problem is that when I Right Click > Save Link As... it doesn't link to the proper image. I'm not sure if its possible to do this or not, but I would really appreciate it if someone could help me on this. Thanks!

+1  A: 

Use a diagnostic tool like Firebug or the DOM Inspector to examine the attribute's actual, final value.

The simplest explanation for differing click and save behaviors is if an event is being intercepted. "Save as" uses the actual href value, whereas selecting the link otherwise can be intercepted (keyboard, mouse down and up, click) to set the location to something else.

Anonymous
A: 

You might be setting this up in $(document).ready() but the fact that you're using $(this) points to your code being bound to the onclick event of the <a>. That would explain why hovering over the link does not show the changed href, the value doesn't get changed until the visitor clicks on the link.

Can you should the associated section of your $(document).ready() to confirm my suspicions?

If I'm correct, then the solution should be to change the href as part of your ready() handling, rather than binding to the onclick event at that time.

Grant Wagner
the $(this) could also be from a each()
Paolo Bergantino
@Paolo: Good point. However, he also says: 'In fact when you mouse over the image the url in the status bar does not read as the one I set with jQuery. But when I click it takes me to the link specified by jQuery.' ... Since changing the href in advance of the onclick should result in the correct URL in the status bar, based on that comment I still suspect he's binding the href change to the onclick event. But I'll be happy to be proven wrong after I see his ready() code.
Grant Wagner
A: 

here's an idea... put the "real" location in the html generated by php

then add an extra attribute with the "fake" location, and then use jquery to switch them on pageload. This can work depending on the situation (ie. it won't ruin everything if the user has js disabled and it uses the "real" location)

this would solve your problem and keep the end result the same for almost all of your users :)

Jiaaro