最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I open a window and run a function on that window? - Stack Overflow

programmeradmin4浏览0评论

I'd like to open a new window, this window has a list of objects, and these objects should be filtered based on a selection from the previous window. I figured I can filter the list through a function, but how do I run said function?

This is what I am able to do:

var popup = window.open('pageURL');
    $(popup.document).ready(function() {
        // this is where function should be
        popup.alert('HelloWorld');
    });

But how do I change the alert to a function?

If I have a function on my other app , function test() { alert('HelloWorld'}; How do I run this function from my first app?

Swapping popup.alert('HelloWorld'); with popup.test(); did not work.

I'd like to open a new window, this window has a list of objects, and these objects should be filtered based on a selection from the previous window. I figured I can filter the list through a function, but how do I run said function?

This is what I am able to do:

var popup = window.open('pageURL');
    $(popup.document).ready(function() {
        // this is where function should be
        popup.alert('HelloWorld');
    });

But how do I change the alert to a function?

If I have a function on my other app , function test() { alert('HelloWorld'}; How do I run this function from my first app?

Swapping popup.alert('HelloWorld'); with popup.test(); did not work.

Share Improve this question edited Apr 9 at 13:15 isherwood 61.2k16 gold badges121 silver badges170 bronze badges asked Jul 6, 2016 at 10:39 schsch 1,3885 gold badges20 silver badges39 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 0

You need the reference to the window opened to call functions in the new window, like:

var oNewWindow = window.open("new.window.url", "mywindow");
oNewWindow.onload = function(){oNewWindow.window.newWindowFunction();};

I ended up with this solution

var popup = window.open('http://s234-0057/actiontracker/SiteAssets/Avvik/html/app.aspx');

var readyStateCheckInterval = setInterval(function() {
    if (popup.document.readyState === "plete") {
        clearInterval(readyStateCheckInterval);
        popup.test();
    }
}, 50);

Where I check if the popup window is ready, and when it is, cancel check and run function. Solution is from top answer on this question, by @this.lau_

You can write it like this:

function myFunction(){
    alert('HelloWorld');
}

var popup = window.open('pageURL');
$(popup.document).ready(function() {
    popup.eval(myFunction + "");
    popup.myFunction();
});

myFunction in file that contains this code will run in page with pageURL address.

发布评论

评论列表(0)

  1. 暂无评论