11

I have a list of links that open in an iframe, like:

<ul id="frameTrigger"> <li><a target="iframe1" href="aaa.html"><img src="aaa.jpg"></a></li> <li><a target="iframe1" href="bbb.html"><img src="bbb.jpg"></a></li> <li><a target="iframe1" href="ccc.html"><img src="ccc.jpg"></a></li> </ul> 

I need to update another div after the link is clicked. Specifically, I need to call a function that checks the frame's src attribute and update the div. If I do a:

$("#frameTrigger a").click(function() { var iframe = $("iframe[name=iframe1]").get(0); console.log(iframe.contentWindow.location.href); // the problem here is that that the iframe.window.location will // change AFTER this function returns }); 

I do not get what is expected.

4
  • 2
    If the links move to a different domain name than yours, you won't be able to pull the SRC. Commented Apr 16, 2011 at 8:11
  • 2
    $("#frameTrigger") you are missing # ... just in case Commented Apr 16, 2011 at 8:14
  • @Khez: you are right, I now see the problem and therefore have to revise the question. Commented Apr 16, 2011 at 8:47
  • @3nigma: I've fixed the typo, but now there is another problem, I'll update the question. Commented Apr 16, 2011 at 8:48

3 Answers 3

9

I'm guessing you get the old value instead of the new one? You may want to wrap your function in a setTimeout of 1ms to allow the update to go through.

Sign up to request clarification or add additional context in comments.

1 Comment

In this particular case a longer timeout (500ms) was needed.
2
$("#frameTrigger a").click(function(){ console.log( $(this).attr("href") ); }); 

You need the first selector to work on clicks for the links. Also, I think you had the syntax for console.log incorrect (though I have never used it). I changed it so when you click on a link it logs the href attribute of the link. Hope this helps.

Comments

0

If you're having trouble with the entire click functionality completing when calling the click via jQuery .click() - I know it's not ideal, but - perhaps you should just have jQuery explicitly complete the sequence that should be executed when clicked. Something like:

$("#someElement").bind('click', function(event) { event.preventDefault(); $($("#frameTrigger a:eq(1)").attr('target')).attr('src', $("#frameTrigger a:eq(1)").attr('href')); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.