Create function in iframe from parent so that it has Function prototype

I have an application where an iframe loads a Blazor application that needs to use a function (invoked from Blazor) available only in the main page. I can add the parent function into the iframe’s window successfully, but the Blazor library performs an instanceof Function check, which for some reason is not true when loading a function to an iframe’s contentWindow this way, even though the function itself is executable.

Is there a way to construct this so instanceof Function returns true? findJsFunction is provided by Blazor and cannot be modified.

I’ve tried:

  • manually setting the prototype to Function
  • using parent.test
  • defining the function test also in the iframe and then overwriting it

main

<iframe id="myFrame" src="url"></iframe>
<script>
    function test() {
        console.info('test');
    }
    document.querySelector('#myFrame').contentWindow.test = test;
</script>

iframe

<!-- blazor library does something like this -->
<script>
    function findJsFunction(name) {
        // the below is what fails because window[name] is never instanceof Function right now
        // is there a way I can get this to return true?
        if (window[name] instanceof Function) {
            window[name]();
        }
    }
</script>
<button onclick="findJsFunction('test')">Click me</button>

1

<iframe id="myFrame" src="url"></iframe>
<script>
    const myObject = {
        test() {
            console.info('test');
        }
    };
    window.myObject = myObject;
    document.querySelector('#myFrame').contentWindow.myObject = myObject;
</script>
<script>
    function findJsFunction(name) {
        if (typeof window[name] === 'object' && typeof window[name].test === 'function') {
            window[name].test();
        }
    }
</script>
<button onclick="findJsFunction('myObject')">Click me</button>

1

Interesting problem, I expect it is to do with context and that your creating a Function in the iframe with the prototype of the parent pages Function object.

I think to get around this you need to have the function created by the JS runtime context in the iframe.

The things I think you can try are injecting a script tag to load a JS file, or you could inject a script tag that contains your JS function.

That was the function will be created with the correct context and should be an instance of the iframe’s Function object.

console.log(Function == document.querySelector('#myFrame').contentWindow.Function)

returns false, so that is likely the cause of your issue.

Using Object.setPrototypeOf to set the prototype of the test function in the iframe, to the Function of the iframe, appears to work:

<iframe id="myFrame" srcdoc="<button onclick="console.log(window.test instanceof Function)">Click me</button>"></iframe>

<script>
    function test() {
        console.info('test');
    }
    document.querySelector('#myFrame').contentWindow.test = test;
    Object.setPrototypeOf(document.querySelector('#myFrame').contentWindow.test,
        document.querySelector('#myFrame').contentWindow.Function)
</script>

That throws security errors here in the snippet, but under https://jsfiddle.net/2j8oLgmt/ I am getting true in the console, when clicking the button in the iframe. (You need to check the real browser console, not the one jsfiddle itself provides.)

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật