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仍然没有显示结果,其空白。我不明白为什么。我什至检查了alert(frame1.src); iframe src设置正确,但未显示结果。

请帮忙

最佳答案

它给我一个错误(search.value未定义)txtSearch输入在哪里?

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


  拒绝在框架中显示“ https://www.google.co.in/search?q=xxx
  因为它将“ X-Frame-Options”设置为“ SAMEORIGIN”。


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

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

关于javascript - 如果从javascript更改,iFrame不会显示结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42725270/

10-12 00:06
查看更多