简体   繁体   中英

Javascript - can I get url of child window?

I need such functional - get URL of child window after it loaded

var ref = window.open(link, window_params);  // link - external in other domain
setTimeout(function(){
    console.log(ref.location.href);
}, 1000); // 100 ms for example

I get error

 Error: Permission denied to access property 'href'

I know about origin restriction to get url from window in another origin and read FAQ after this article - https://developer.mozilla.org/en-US/docs/Web/API/window.open?redirectlocale=en-US&redirectslug=DOM%2Fwindow.open

Can i get url of opened window in some way? Any ideas?

If the opened page is within your control, you should be able to set up a callback function in the parent which the child window calls;

Parent

window.OnChildWindowLoaded = function (href) {
    console.log('Opened ' + href + ' successfully');
};
window.open('Child.html', ...);

Child.html

<script>
window.onload = function () {
    window.opener.OnChildWindowLoaded(location.href);
};
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM