简体   繁体   中英

Passing the data within an input field between frames

I created a frameset that is composed of three frames. The first one takes the user's name. The second frame, contains a button called greeting. And the third frame outcome.html should outcome: Hello (user's name) upon clicking on the greetings button. Any help on how to pass the name of the user to the outcome frame upon clicking on the greeting button, to display it as "Hello ..." using javascript.

Frameset:

<frameset rows="34%,33%,33%">
    <frame src="name.html" name="name">
    <frame src="greeting.html" name="greeting">
    <frame src="outcome.html" name="outcome">
    </frameset>

Name.html :

<body>
    <div id="fname">
        <form>
            Client Name: <input type="text" name="fname" id="fname">
        </form>
     </div>
</body>

Greeting.html:

<div id="greeting">
        <form>
            <button type="button">Greeting</button>
        </form>
     </div>

You can save yourself some time and use jQuery (even if I won't recommend the use of heavy libraries to perform simple DOM manipulations for small tasks, but in your case I guess you just need to get the job done).

Use the function .contents() to get the content of the iframe

Proceed this this :

var nameFrame = $('frame[name=name]').contents();
var outcomeFrame = $('frame[name=outcome]').contents();
var greetingFrame = $('frame[name=greeting]').contents();

$(greetingFrame).on('click', 'button', function() {
  var name = $(nameFrame).find('.fname').val();
  $(geetingFrame).text(name);
});

Frames are accessible via their name attribute on the top parent frame.

So for example from within a frame:

parent.greeting.document.getElementById('fname').value

Here is a working example: http://embed.plnkr.co/fMMCGxkR0zwgo3nCqhC5/preview

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM