3

Let's say I have a button right next to an Iframe and in that Iframe is a download link. Is it possible that when I click the outside button, the download link on the iframe will be clicked?

http://jsfiddle.net/idude/9RQ3N/

<button type="button">Click Me!</button>

<iframe src="http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_a_download" height="400" width="800">

For example, in the jsfiddle link above, how could I make it so that the w3 picture would be downloaded by just clicking the button?

Thanks in advance!

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
idude
  • 4,654
  • 8
  • 35
  • 49
  • 3
    Not possible for security reasons. Take a look at [CSRF](http://en.wikipedia.org/wiki/Cross-site_request_forgery) – ssergei Jul 11 '14 at 00:24

4 Answers4

3

Try (this pattern)

$(function () {

    var url = ["http://example.org"]; // `url`, e.g., `http://example.org`
    var iframe = $("<iframe>", {
            "id": "frame",
            "width": "400px",
            "height": "300px"
    });
    $.when($("body").append(iframe))
        .then(function (b) {

        var img = $("<img>", {
                 "id" : "frameimg",
                "width": "400px",
                "height": "300px",
        }).css("background", "blue");
             var a = $("<a>", {
                "href" : url[0],
                "html" : img
            }).css("textDecoration", "none");
        var button = $("<button>", {
            "html": "click"
        }).css("fontSize", "48px")
            .one("click", function (e) {
            $(b).find("#frame").contents()
                .find("body a")
                .get(0).click()
        });
        $(button).appendTo(b);
        $(b).find("#frame").contents()
            .find("body").html(a);
    });

});

jsfiddle http://jsfiddle.net/guest271314/2x4xz/

guest271314
  • 1
  • 15
  • 104
  • 177
2

To access the iFrame DOM, all you need to do is :

  var x = document.getElementById("myframe");
  var y = x.contentWindow.document; 

or you can get that iFrame by using

  window.frames[0] 

or something like that if you prefer not to use an ID for iFrame.

and then you can search for something in that document and trigger a click event on that element.

  y.getElementById("somButton").click();
Ash
  • 2,575
  • 2
  • 19
  • 27
  • 2
    You can use `contentDocument` instead of `contentWindow.document`. But of course, not cross-domain. – Oriol Jul 11 '14 at 00:32
  • yebo, in cross domain it gonna throw error like "Permission denied to access property" or something i think. – Ash Jul 11 '14 at 00:36
1

Mozilla's JSChannel is designed for communicating to/fro an iframe using POST. You should consider checking it out.

An example communiqué

The parent HTML could be

<script src="jschannel.js"></script>
<iframe id="childId" src="child.html"></iframe>
<script>
    var myChildChannel = Channel.build(document.getElementById("childId").contentWindow, "*", "testScope");
    myChildChannel.query({
        method: "myMethodName",
        params: "I'm Bob!",
        success: function(v) {
            alert(v);
        }
    });
</script>

Now for the child:

<script src="jschannel.js"></script>
...
<script>
    var myParentChannel = Channel.build(window.parent, "*", "testScope");
    myParentChannel.bind("myMethodName", function(t, param) {
        return 'you said "'+param+'"';
    });
</script>

This, when run, will produce and alert saying you said "I'm Bob!"

Obviously, you can do a lot with this. Have fun :)

lucasem
  • 481
  • 3
  • 12
  • 1
    Unless I'm mistaken, this only works when you control the contents of the iframe. OP was linking to w3schools in his example, which I assume he does not own. – Dan Hlavenka Jul 11 '14 at 04:08
0

As correctly pointed by @ssergei, this is not possible for security reasons. This is the jsFiddle I used for catching the errors: http://jsfiddle.net/9RQ3N/5/.

This is the SecurityError message that Google Chrome throws:

Uncaught SecurityError: Failed to read the 'contentDocument' property from
'HTMLIFrameElement': Blocked a frame with origin "http://fiddle.jshell.net"
from accessing a frame with origin "http://www.w3schools.com".
Protocols, domains, and ports must match.

From Firefox:

Error: Permission denied to access property 'document'

And from Safari:

Blocked a frame with origin "http://fiddle.jshell.net" from accessing a frame
with origin "http://www.w3schools.com". Protocols, domains, and ports must match.
Community
  • 1
  • 1
danilopopeye
  • 9,610
  • 1
  • 23
  • 31