开发者

Trigger javascript on child from parent window, and run in child context

开发者 https://www.devze.com 2023-04-02 17:38 出处:网络
So, I am trying to open a second window, so that a download can be triggered from it, rather than the current form.

So, I am trying to open a second window, so that a download can be triggered from it, rather than the current form.

In the parent window, I open a child window with:

var windowHandle = window.open("../Downloading.aspx", "_blank", "height=200, width=200,alwaysRaised=yes", false);

I then add a form (downloadReportForm is a jQuery object with the form) to the downloading.aspx page

$(windowHandle.document).ready(function () {
    var $oldForm = $('form', this);
    alert($oldForm.length); //1

    $oldForm.after(downloadReportForm);
    var $excelReport = $('form#excelReport', this);

    alert($excelReport.length); //1
    $excelReport.submit();
});

It looks like it works, however, it still seems to trigger the form as though it is coming from the parent window, rather than the child.

What I am trying to accomplish is a small "please wait for your download" window. The form has to be generated client side, and then triggered. It works if it's con开发者_Go百科tained in one page, but when I try and put the form in the newly created/opened window, it doesn't run in the new window.

Any idea how I can perform this feat?

EDIT: It looks like the problem is actually with appending the form to the new window...it's being appended to the current window, not the new one.

EDIT2: Not sure why I can't append this form to the body...

$(windowHandle.document).ready(function () {
    var $body = $(windowHandle.document.body);

    //FAILING to append...getting an exception
    $body.append(downloadReportForm);

    var $excelReport = $('form#excelReport', $body);                    
    $excelReport.submit();
});

The jQuery variable $body has a length of 1. But the append line fails saying "no such interface".


From the child, use opener. before your function call.

example: opener.alert("moo")


Turns out the problem is that I wasn't creating the downloadReportForm properly

I was just creating elements using $('<input ... />'); when if I want to add them to another document, they need to be created in that documents context using $('<input ... />', windowHandle.document);

0

精彩评论

暂无评论...
验证码 换一张
取 消