Check if window parent is same domain

后端 未结 2 1880
栀梦
栀梦 2021-01-12 23:47

My problem is that I\'m creating an application that will be added to different pages in an iframe. But sometimes it will be on my own domain and sometimes on some one elses

相关标签:
2条回答
  • 2021-01-13 00:05

    You could use:

    try{
        parent.document;
        // accessible
    }catch(e){
        // not accessible
    }
    
    0 讨论(0)
  • 2021-01-13 00:12

    I tried checking parent document, but for some reasons in new iOS devices (chrome or safari) it didn't throw the exception, but just continued to the next step.

    So I took it another step:

    try{
        var doc = parent.document;
        if(!doc)
            throw new Error('Unaccessible');
        // accessible
    }catch(e){
        // not accessible
    }
    
    0 讨论(0)
提交回复
热议问题