如何在新窗口中编写JavaScript(window.open())?

时间:2015-05-15 23:04:24

标签: javascript

对于JavaScript分配,我将使用window.open()创建一个新窗口。此窗口将使用document.write()填充内容。该窗口必须具有使用JavaScript来改变窗口中文本大小的按钮。

我的问题是,如何在这个新窗口中使用或编写JavaScript,以便我可以调整窗口的文本大小?

这是我的代码的缩短版本(存储在外部js文件中):



var newWindow;

function displayWindow() {
  newWindow = window.open("", "newWindow", "width=800, height=600");
  newWindow.document.write('<button onclick="resizeText(1)">-</button> Text size <button onclick="resizeText(2)">+</button>');
  newWindow.document.write('Here is some text that I would like to be able to be resized.');
}

function resizeText(change) {
  switch (change) {
      case 1:
        newWindow.document.style.fontsize = "80%";
        break;
      case 2:
        newWindow.document.style.fontsize = "120%";
        break;
      default:
        alert('Error');
        break;
    }
}
&#13;
&#13;
&#13;

如何在document.write()中访问我的JavaScript函数?我也尝试在document.write()语句中编写函数,但这也不起作用。

谢谢Mark。

2 个答案:

答案 0 :(得分:1)

此代码基于您已经拥有的内容:

import collections
import string

def translator(frm='', to='', delete='', keep=None):
    # Python Cookbook Recipe 1.9
    # Chris Perkins, Raymond Hettinger
    if len(to) == 1: to = to * len(frm)
    trans = string.maketrans(frm, to)
    if keep is not None:
        allchars = string.maketrans('', '')
        # delete is expanded to delete everything except
        # what is mentioned in set(keep)-set(delete)
        delete = allchars.translate(allchars, keep.translate(allchars, delete))
    def translate(s):
        return s.translate(trans, delete)
    return translate

alpha = 'abcdefghijklmnopqrstuvwxyz'
keep_alpha=translator(keep=alpha)

while True:
    speech = raw_input("Enter file name:")
    wholeFile = open(speech, 'r+').read()
    lowlet = wholeFile.lower()
    letters = keep_alpha(lowlet)
    n = len(letters)
    occurrences = collections.defaultdict(int)    
    for x in letters:
        occurrences[x]+=1
    for x in occurrences:
        print x, occurrences[x]

答案 1 :(得分:0)

马克 -

如果未阻止弹出窗口,则下面的示例有效。这只是您可以做到这一点的众多方法之一。请注意,它使用“opener”在父窗口中调用该函数,然后更改正文的字体大小。您的代码正在尝试设置文档的字体大小,这不是您想要的,即它不会执行任何操作。另请注意,fontSize区分大小写,如果使用“fontsize”,则不会发生任何事情。最后,在设置正文字体大小时不要使用百分比,因为它不会像您期望的那样工作。而是使用px(像素)。无论如何,这应该足以让你去,我会把细节留给你。

<html>
<body onload="init()">
<h2>Parent Window</h2>
 <script type="text/javascript">

     var popup;

     function init() {
        popup =  window.open("", "newWindow", "width=800, height=600");
        if (popup) popup.document.write('<html><body>Child Window<br><button onclick="opener.fontSize(48)">Test</button></body></html>'); 
     }

     function fontSize(size) {
         popup.document.body.style.fontSize = size + 'px';
     }

</script>
</body>
</html>