Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to transfer jquery data between iframes

So I have two iframes in page

<div id="ch">
    <iframe name="users" width="220" height="510" align="left" src='messages/users.php' id="userch" ></iframe>
    <iframe name="text"  width="450" height="405" src='messages/text.php'></iframe>
</div>

So I have variables in first iframe and I need to sent them into second iframe. I tried to use method GET. But any method that will work is good.

so first iframe

        <html>
    <head>
    <meta http-equiv="Content-type" content="text/html; charset=UTF-8">
    <meta content="10; url=users.php" HTTP-EQUIV=Refresh>  
    <meta content="utf-8" http-equiv="encoding">
    <link href="../css/chat.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="../js/jquery-2.1.3.js"></script>
    <script type="text/javascript" src="../js/ch_users.js"></script>
    </head>
    <body >
<div class="ess_contact">

<div>
    </body>

    </HTML>

2-nd iframe

<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">

<meta content="utf-8" http-equiv="encoding">  
<meta content="5; url=text.php?active=<?=$active;?>"  HTTP-EQUIV=Refresh> 
</head>
<body onload="scroll(0,100)" link="blue" alink="blue" vlink="blue">
<font size=3 face="Georgia">
<?php

    if (isset($_GET['active']))
    { 
        $active=$_GET['active'];
        echo $active;
        }
?>
</body>

</html>

and js file

jQuery.noConflict();
jQuery(document).ready(function($) {

$(document).on('click','.ess_contact', function () {

        var active = this.id;   
        $.get( "text.php", { active: active} );

});
});
like image 236
ZeroVash Avatar asked Nov 18 '25 08:11

ZeroVash


1 Answers

I tried transferring data between iframes, and this is how I did it.

Main.html

<html>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<body>
    <h2>Iframe data test</h2><br/>
    <h2>Iframe 1</h2>
    <iframe src="f1.html" id="frame1"></iframe>
    <br/>
    <br/>
    <h2>Iframe 2</h2>
    <iframe src="f2.html" id="frame2"></iframe>
</body>
</html>

f1.html

<html>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<body>
    <h2>Iframe 1 here</h2><br/>
    <input type="text" id="mydata"/><br/>
    <input type="button" id="send" value="send data"/>
</body>

<script>
$(document).ready(function(){
    $("#send").click(function(){
        parent.$("#frame2").contents().find("#target").html($("#mydata").val());
    });
});

</script>
</html>

f2.html

<html>
<body>
    <h2>Iframe 2 here</h2><br/>
    <div id="target"></div>
</body>
</html>

You see the two iframes on the Main page. Type anything in the textbox and click Send data, the contents of the textbox will be set in a div found in the second iframe.

like image 104
Ahs N Avatar answered Nov 19 '25 22:11

Ahs N



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!