无法在使用Chrome的Iframe中调用函数或postMessage

时间:2017-05-21 12:57:37

标签: javascript google-chrome iframe

Helly everyone,

我拼命试图在iframe中调用一个函数几个小时而没有成功。

以下是我拥有的两件事(使用Safari),父代码:

#!/usr/bin/python

import re           # String matching
import collections  # collections.Counter basically solves your problem


def loadwords(s):
    """Find the words in a long string.

    Words are separated by whitespace. Typical signs are ignored.

    """
    return (s
            .replace(".", " ")
            .replace(",", " ")
            .replace("!", " ")
            .replace("?", " ")
            .lower()).split()


def loadwords_re(s):
    """Find the words in a long string.

    Words are separated by whitespace. Only characters and ' are allowed in strings.

    """
    return (re.sub(r"[^a-z']", " ", s.lower())
            .split())


# You may want to read this from a file instead
sourcefile_words = loadwords_re("""this is a sentence. This is another sentence.
Let's write many sentences here.
Here comes another sentence.
And another one.
In English, we use plenty of "a" and "the". A whole lot, actually.
""")

# Sets are really fast for answering the question: "is this element in the set?"
# You may want to read this from a file instead
keywords = set(loadwords_re("""
of and a i the
"""))

# Count for every word in sourcefile_words, ignoring your keywords
wordcount_all = collections.Counter(sourcefile_words)

# Lookup word counts like this (Counter is a dictionary)
count_this = wordcount_all["this"] # returns 2
count_a = wordcount_all["a"] # returns 1

# Only look for words in the keywords-set
wordcount_keywords = collections.Counter(word
                                         for word in sourcefile_words
                                         if word in keywords)

count_and = wordcount_keywords["and"] # Returns 2
all_counted_keywords = wordcount_keywords.keys() # Returns ['a', 'and', 'the', 'of']

window.frames['map-frame'].Test();

框架代码:

document.getElementById('map-frame').contentWindow.postMessage("test", '*');

window.Test = function ()
 {
  alert("1");
 }

正如我所说,它适用于Safari中的两种解决方案,但不适用于Chrome。这两个文件都在我的网络服务器上(我已经看到可以在Chrome中阻止本地通话)。我在Stackoverflow和谷歌上找到的任何东西都没有尝试过,但这些技巧都没有。

有没有人遇到同样的问题?有人知道一些技巧或尝试吗?

先谢谢人们

1 个答案:

答案 0 :(得分:0)

https://stackoverflow.com/a/40407241/3316068

William Littlefield的解决方案为我工作。另外,在从中调用函数之前,请确保您的框架已完全加载。