如果从javascript更改,iFrame不显示结果

时间:2017-03-10 18:21:47

标签: javascript html iframe

javascript代码:

<script>

    function searchall()
    {
        var search = document.getElementById("txtSearch");
        var frame1 = document.getElementById("frame1");

        frame1.src = "https://www.google.co.in/search?q=" + search.value;
        alert(frame1.src);
    }

</script>

html代码:

<asp:TextBox ID="txtSearch" runat="server" class="form-control my-txt-box" ></asp:TextBox>
    <input id="btnAll" class="my-btn" onclick="searchall()" type="button" value="Search All" />

        <iframe id="frame1" src="home.aspx">
            does not support
        </iframe>

我的代码似乎是正确的,但iFrame仍然没有显示结果,它的空白。我不明白为什么。我甚至用警告(frame1.src); iframe src设置正确但没有显示结果。

请帮助

2 个答案:

答案 0 :(得分:3)

它给我一个错误(search.value undefined)在哪里是txtSearch输入?

如果我输入txtSearch输入,则会给出以下错误:

  

拒绝展示&#39; https://www.google.co.in/search?q=xxx&#39;在一个框架中   因为它设置了X-Frame-Options&#39;到&#39; SAMEORIGIN&#39;。

来自google.com的HTTP响应标题包含:

x-frame-options:SAMEORIGIN

它会禁用iframe中的页面呈现。请参阅:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options

http://codepen.io/anon/pen/MppZGX

修改 谷歌不再支持用于显示搜索结果的iframe托管选项。请参阅:https://support.google.com/customsearch/answer/4542055?hl=enhttps://support.google.com/customsearch/answer/2641279?hl=en

答案 1 :(得分:0)

尝试在功能结束时刷新iframe:

var iframe = document.getElementById('youriframe');
iframe.src = iframe.src;

此外,通过“btnAll”切换var“搜索”ID。 “txtSearch”不在您的HTML上。