简体   繁体   English

javascript document.write脚本

[英]javascript document.write script

Edited qestion 编辑问题

I'm trying to create two separate HTML documents: main.html and sufler.html . 我正在尝试创建两个单独的HTML文档: main.htmlsufler.html Idea is to control sufler.html page from main.html . 想法是从main.html控制sufler.html页面。 To do that I found a solution to write sufler's.html code like string element on main.html page, change what I need in that string element and write it with document.write function from main.html . 为此,我找到了一种解决方案,可以在main.html页面上编写诸如字符串元素之类的sufler's.html代码,更改所需的字符串元素,并使用main.html中的 document.write函数进行编写。 Everything works fine except <script> function... <script>功能外,其他所有功能都正常运行...

main.html main.html

<script type="text/javascript">
var HTMLstringPage1     = '<!DOCTYPE html><html><head><link href="stilius.css" rel="stylesheet" type="text/css" />',
    HTMLstringPage2     = '</body></html>',
    HTMLstringStyle     = '\x3Cscript type="text/javascript">function styluss(){document.getElementById("flip").style.font="normal normal 30px sans-serif";}\x3C/script>',
    HTMLstringDiv1      = '</head><body onload="styluss()"><div id="sufler"><div id="mov"><p id="flip">',
    HTMLstringDiv2      = '</p></div></div>';

var newWindow           = window.open('sufler.html','_blank','toolbar=no, scrollbars=no, resizable=no, height=615,width=815');

    newWindow.document.body.innerHTML = '';
    newWindow.document.write(HTMLstringPage1,HTMLstringDiv1+"Text"+HTMLstringDiv2,HTMLstringPage2); //works fine
//  newWindow.document.write(HTMLstringPage1,HTMLstringStyle,HTMLstringDiv1+"Text"+HTMLstringDiv2,HTMLstringPage2);//works except script function
</script>

Can someone help on this? 有人可以帮忙吗?

You have to use normal opening brackets for <script> tags: 您必须对<script>标签使用普通的方括号:

var HTMLstringStyle = '<script type="text/javascript">function styluss(){document.getElementById("flip").style.font="normal normal 30px sans-serif";}<\/script>';

Although I can't see why you would ever use this... attaching a <script> tag to the <head> or the <body> is almost always a superior solution. 尽管我看不到您为什么会使用此方法...将<script>标记附加到<head><body>几乎总是一个更好的解决方案。

To dynamically add a script tag, it is much better to do this: 要动态添加脚本标签,最好这样做:

var newDoc = newWindow.document;
var script = newDoc.createElement("script");
script.type = "text/javascript";
var text = newDoc.createTextNode('function styluss(){document.getElementById("flip").style.font="normal normal 30px sans-serif";}');
script.appendChild(text);
newDoc.getElementsByTagName("head")[0].appendChild(script);

Working demo: http://jsfiddle.net/jfriend00/JqW5F/ 工作演示: http : //jsfiddle.net/jfriend00/JqW5F/


But, creating code on the fly like this is almost never needed. 但是,几乎不需要像这样即时创建代码。 Since you must, by definition, generally already know what you want the code to do, you can just have a pre-created function that takes some arguments and then call that existing function with the right arguments to accomplish what you wanted to rather than creating a custom function on the fly: 根据定义,既然您通常必须已经知道要执行的代码,那么您可以拥有一个预先创建的函数,该函数需要一些参数,然后使用正确的参数调用该现有函数来完成您想要的操作,而不是创建即时定制功能:

function styleIt(id, fontStyle) {
    document.getElementById(id).style.font = fontStyle;
}

styleIt("flip", "normal normal 30px sans-serif");
styleIt("flip2", "normal normal 12px sans-serif");

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

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