1

javascript code:

<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 code:

<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>

my code seems to be correct but still iFrame does not show the result, its blank. I dont understand why. I even checked with alert(frame1.src); iframe src is setting correct but not showing result.

please help

Arnold Parge
  • 6,684
  • 2
  • 30
  • 34

2 Answers2

3

It gives me an error (search.value undefined) where is the txtSearch input ?

If I put a txtSearch input it gives me the folowing error:

Refused to display 'https://www.google.co.in/search?q=xxx' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.

The header of HTTP response from google.com has:

x-frame-options:SAMEORIGIN

It disables the page rendering in an iframe. See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options

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

EDIT: The iframe hosting option for displaying search results is no longer supported by google. See: https://support.google.com/customsearch/answer/4542055?hl=en and https://support.google.com/customsearch/answer/2641279?hl=en

F.Igor
  • 4,119
  • 1
  • 18
  • 26
  • ok, I got you, so is there any other way to achieve what I want.?? – Arnold Parge Mar 10 '17 at 18:36
  • 1
    See http://stackoverflow.com/questions/6666423/overcoming-display-forbidden-by-x-frame-options . This could be possible if you add a header in your main page. – F.Igor Mar 10 '17 at 18:49
0

Try to refresh your iframe on function ending:

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

Also, switch var "search" ID by "btnAll". The "txtSearch" is not on your HTML.

  • @ArnoldParge your variable it's not right: var search = document.getElementById("txtSearch");. There is no input with ID called txtSearch, you put "btnAll". Try to set the same. – felipekrust Mar 10 '17 at 18:29