简体   繁体   中英

Several document.write in iframe in Edge cause SCRIPT70: Permission denied

The document.write causing the error SCRIPT70: Permission denied if you use a few document.write inside iframe. This error occurs only in the Edge. In all other browsers this error is not observed. For example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SSP-558</title>
</head>
<body>
<script>
    document.write("1");
    document.write("2");
</script>
<script>
    var iframe = document.createElement("iframe");
    iframe.width = window.outerWidth;
    iframe.height = window.outerHeight;
    iframe.onload = function () {
        var doc = iframe.contentWindow.document;
        var script = doc.createElement("script");
        script.type = "text/javascript";
        script.text = [
            'document.write("1");',
            'document.write("2");'
        ].join("");

        doc.body.appendChild(script);
    };
    document.body.appendChild(iframe);
</script>
</body>
</html>

This code will display 12 on the page and in iframe, but it cause an error and displays only 1 inside iframe in Edge.

If you use only one document.write everything works fine. Unfortunately, the code that contains several document.write comes from a third-party developers and they can't change it.

Have you encountered this error and is there any solution for it?

There was found a solution. If you add window in front of document.write the error will not be caused.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SSP-558</title>
</head>
<body>
<script>
    document.write("1");
    document.write("2");
</script>
<script>
    var iframe = document.createElement("iframe");
    iframe.width = window.outerWidth;
    iframe.height = window.outerHeight;
    iframe.onload = function () {
        var doc = iframe.contentWindow.document;
        var script = doc.createElement("script");
        script.type = "text/javascript";
        script.text = [
            'window.document.write("1");',
            'window.document.write("2");'
        ].join("");

        doc.body.appendChild(script);
    };
    document.body.appendChild(iframe);
</script>
</body>
</html>

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