0

Let's say I have an embedded web page on a website (Example: www.en.wikipedia.org/wiki/Pulsar). The user than clicks on a hyperlink inside the article (Ex: https://en.wikipedia.org/wiki/Extrasolar_planet).

I want to have a frame which displays the name of the article (in this case, "Extrasolar planet") when the user clicks an entry from the original article ("Pulsar")

How do I know what the user has clicked?

EDIT: After seeing many examples, I tried to do this instead, but it doesn't quite work.

<iframe width="940" height="550" src="http://en.wikipedia.org/wiki/Special:Random">    </iframe>


<script>
(document.getElementsByTagName('iframe').contentWindow.document).keydown = function() {
alert("Hello World")
})
</script>
carriwitchet
  • 121
  • 1
  • 1
  • 10
  • 2
    You can attach event-listeners in an iframe like [this](http://stackoverflow.com/a/550247/2171046). – guid Jul 19 '13 at 09:32

2 Answers2

1

This doens't work because you're using getElementsByTagName which returns an array. Use getElementById or getElementsByTagName('iframe')[0] instead.

guid
  • 82
  • 2
0

contentWindow is the iframe's window object. Here you want the iframe's document:

$(document.getElementById('iframe_id').contentWindow.document).keydown(function() {
    // my func
});

OR

  $(document.getElementById('iframe_id').contentWindow.document).click(function() {
        // my func
    });
Optimus Prime
  • 6,817
  • 5
  • 32
  • 60