I have the following jQuery code which doesn’t seem to be doing anything. When the link is clicked, I’m trying to get the value of its url attribute to be copied into the iframe‘s src attribute.
Basically this is supposed to make links open up in an iframe (the content of the iframe is replaced every time a new link is clicked) instead of acting like normal links.
So what is wrong with my code?
$('p.tweet a').click(function(){
var link = $(this).attr('url');
$('iframe').attr('src',link);
})
You are not canceling the default action by returning false from the click handler of the anchor you subscribed to and thus not leaving any chance for your script to execute before the browser simply redirects away from that page:
And by the way notice that I’ve used
$(this).attr('href')instead of$(this).attr('url'). Thehrefattribute is kinda more common to anchors than theurlattribute in standard HTML, but if you have invented some markup in which anchors have anurlattribute then you could give it a spin.