How to add an onclick event from an iframe to its parent window in plain JS?

1

I'm trying to add an onclick event to a link, from inside an iframe. The link is located in the parent window of the iframe. The following code is what I'm using, but I'm getting an Exception. I'm testing on Firefox 9.0.1

function creaURL(url){
    //Some code to generate the string cadenaURL and give it the right format, it contains the data to pass
    this.location.href = url + '?' + encodeURIComponent(cadenaURL);
}

//This is where I'm adding the event to the link in the parent window
parent.document.getElementById("link7").onclick = function(){ creaURL("exercici7.htm") };

I've been able to make it work when the link is inside the iframe, but when the link is in the parent window, I just won't work. The Exception, in case it may be of some help:

Error: uncaught exception: [Exception... "prompt aborted by user"  nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)"  location: "JS frame :: resource:///components/nsPrompter.js :: openTabPrompt :: line 462"  data: no]

Thanks in advance.

javascript
iframe
javascript-events
asked on Stack Overflow Jan 3, 2012 by Ramon Royo

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0