Can a button click cause an event in iframe?

2019-01-09 18:58发布

问题:

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!

回答1:

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/



回答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();


回答3:

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 :)



回答4:

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.