How to tell if a window exists in Javascript?

前端 未结 4 1971
情话喂你
情话喂你 2020-12-30 05:07

I\'ve seen ways of seeing if a window a particular script opened is still opened, but what if it didn\'t?

I have a small window that has a button to click to load th

4条回答
  •  一个人的身影
    2020-12-30 05:37

    Use try{} and catch(err){}.

    try{
    window.opener.document.body.getElementById('#elementId'); // do some action with window. opener
    }
    catch(err){
    // if we are here then probably there is no window.opener
    window.close(); // closing this window or whatever you want
    }
    

提交回复
热议问题