Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.parent.document working in firefox , but not in chrome and IE

My concept is to update the value of the text box in the main page from the iframe . This code is working in firefox , but not working in Internet Explorer and Chrome . Both main.html and frame.html are in same location . I need suggestions to make it work in all the browsers .

main.html

<!DOCTYPE html>
<html>
<head>
<title> main window </title>
</head>
<body>
Parent textbox :<input type="text" id="parentbox"></br></br></br>
<iframe src="frame.html" ></iframe>
</body>
</html>

frame.html

<!DOCTYPE html>
<html>
<head>
<title> frame window </title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
function PushValues()
{
 var frame_value = document.getElementById("framebox").value;
 window.parent.document.getElementById("parentbox").value =frame_value;
}
</script>
</head>
<body>
<input type="text" id="framebox" >
<input type="button" value ="fill" onclick="PushValues()">
</body>
</html>
like image 523
Nishanth Lawrence Reginold Avatar asked Jul 22 '13 07:07

Nishanth Lawrence Reginold


3 Answers

As per security policies, cross-domain access is restricted. This will happen if you are trying to show a page from domain 1 in domain 2 and try to manipulate the DOM of page in domain 2 from the script in domain 1. If you are running the pages from same location on a server. This shouldn't happen. However, if you are just saving them as HTML files and trying to open them in your browser, it should not work. I have created two jsbins for your code and it is working on chrome. Try to access them using the below links.

Main.html: http://jsbin.com/afazEDE/1 iframe.html: http://jsbin.com/ayacEXa/1/

Try to run main.html in edit mode in JSBin by keeping console open in chrome (F12) and click fill button. It will not work and will show you the error. If you run them as it is (in run mode of JSBin), it will work.

like image 200
Abhishek Chaitanya Gorlagunta Avatar answered Sep 21 '22 15:09

Abhishek Chaitanya Gorlagunta


Jquery -

function PushValues()
{
  var frame_value = $('#framebox').val();
  parent.$('body').find('#parentbox').val(frame_value);
}

It's always work for me.

like image 28
Ishan Jain Avatar answered Sep 21 '22 15:09

Ishan Jain


Run this code on a server like xamp or wamp it wont work directly

Main.html

<!DOCTYPE html>
<html>

<head>
    <title> main window </title>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>

<body>
    Parent textbox :<input type="text" id="parentbox" value=""></br></br></br>
    <iframe src="iframe.html"></iframe>
    <script>
        window._fn = {
            printval: function (response) {
                $("input").val(response);
            },
        };
    </script>
</body>

iframe

<!DOCTYPE html>
<html>
<head>
    <title> frame window </title>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>

<body>
    <input type="text" id="framebox">
    <input type="button" value="fill" onclick="PushValues()">

    <script language="javascript">
        function PushValues() {
            window.parent._fn['printval']($('input').val());
        }

    </script>
</body>

</html>
like image 21
Richie Fredicson Avatar answered Sep 18 '22 15:09

Richie Fredicson