简体   繁体   English

在使用 document.write 创建的文档中调用 javascript 函数

[英]Calling a javascript function in a document created with document.write

I have a web page with a button.我有一个带有按钮的网页。 The click code is:点击代码为:

var html = ...html string containing visual and script elements...
var view = window.open();
view.document.write(html);
view.init(<parameters>); // see next code block

the html content is: html内容是:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style type="text/css">
        html, body {
            height: 100%;
            margin: 0;
        }
    </style>
</head>
<body>
    <div id="id1"></div>
    <script>
        function init(<parameters>) {
             ...work...
        }
    </script>
</body>
</html>

The problem is with the init function call in chrome: all good if I am in IE, but in chrome I get "init function not defined" exception.问题在于 chrome 中的 init 函数调用:如果我在 IE 中,一切都很好,但是在 chrome 中,我得到“未定义 init 函数”异常。 How should I do to get this working in all browsers?我应该怎么做才能让它在所有浏览器中都能正常工作? Of course I am looking for a solution that doesn't require a server round trip.当然,我正在寻找一种不需要服务器往返的解决方案。

IM a noob so idk if this is exaclty true but i have read that ie allows you to do alot more then chrome or firefox.我是个菜鸟,所以 idk 如果这是真的,但我已经读过,即允许你做更多的事情,然后 chrome 或 firefox。 It might be one of those example where ie will let you do something.它可能是 ie 会让你做某事的例子之一。

using document.write does in fact work when it comes to create the page I want.在创建我想要的页面时,使用 document.write 实际上确实有效。 Problem is when I want to call a function defined in a javascript block inside that page.问题是当我想调用在该页面内的 javascript 块中定义的函数时。 Different browsers give different results so I guess this is a matter not completely standardized yet.不同的浏览器给出不同的结果,所以我想这是一个尚未完全标准化的问题。 There are posts in the internet about this, but I couldn't find a clear and common answer.互联网上有关于此的帖子,但我找不到明确且常见的答案。 I then solved my impasse with a workaround.然后我用一种解决方法解决了我的僵局。 The initial markup contains now placeholders for the parameters:初始标记现在包含参数的占位符:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style type="text/css">
        html, body {
            height: 100%;
            margin: 0;
        }
    </style>
</head>
<body>
    <div id="id1"></div>
    <script>
        (init = function () {
             var parameter1 = ${{placeholder1}}
             var parameter2 = ${{placeholder2}}
             ...
             ...work...
        })();
    </script>
</body>
</html>

The creating code, then, replaces the placeholders with actual values:然后,创建代码用实际值替换占位符:

var html = ...html string containing placeholders...
html = html.replace("${{placeholder1}}", actual1);
html = html.replace("${{placeholder2}}", actual2);
...
var view = window.open();
view.document.write(html);

Now the init function is called in the same page context, and this works in Chrome as well.现在 init 函数在同一个页面上下文中被调用,这也适用于 Chrome。

It is not possible to write to a new window if its not on the same domain.如果不在同一个域中,则无法写入新窗口。 What I suggest is that you can open an iframe an work inside that.我的建议是你可以打开一个 iframe 里面的一个作品。

How to write to iframe 如何写入 iframe

How to write to page on same domain 如何写入同一域中的页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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