Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
593 views
in Technique[技术] by (71.8m points)

javascript - passing input from window.open to parent page

I have created a frameset, in which one of its frames open a pop-up using window.open . In that window opened i have a form to collect input from users. Input which i'm trying to return to the parent but failing to. Any help. I tried using window.opener using this solution :Popup window to return data to parent on close but failed to get the getChoice() outcome back to the parent page.

The frame:

<form>
<button type="button" id="opener" onClick="lapOptionWindow('form.html','', '400','200');">Opinion </button>
</form>

the frame js:

function lapOptionWindow(url, title, w, h) {
    var left = (screen.width/2)-(w/2);
    var top = (screen.height/2)-(h/2);
    return window.open(url, title,'toolbar=no,location=no,directories=no, status=no, menubar=no, scrollbars=no,resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
} 

form.html (the window opened):

<div id="lightbox">
            <strong>Chinese/Portuguese: Chinese<input type="radio" name="chorpor" value="" id="choice1"> Portuguese<input type="radio" name="chororpor" value="" id="choice2"></strong>
            </br><button type="button" id="food" onclick="getChoice()">Submit</button>
    </div>

form.js:

function getChoice() {
        var choice = ""; 
        var choice1 = document.getElementById("choice1 "); 
        choice = choice1.checked == true ? "Chinese" : "Portuguese";
        return choice;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a proposal (I changed small things, please be careful):

Body of parent.html:

<button type="button" onclick="popup('popup.html', '', 400, 200);">Opinion</button>
=&gt;
<span id="choiceDisplay">No choice.</span>

JavaScript of parent.html:

function popup(url, title, width, height) {
  var left = (screen.width/2) - (width/2);
  var top = (screen.height/2) - (height/2);
  var options = '';

  options += 'toolbar=no,location=no,directories=no,status=no';
  options += ',menubar=no,scrollbars=no,resizable=no,copyhistory=no';

  options += ',width='  + width;
  options += ',height=' + height;
  options += ',top='    + top;
  options += ',left='   + left;

  return window.open(url, title, options);
}

function setLang(choice) {
  var languages = {
      'en': 'English',
      'fr': 'French',
      'ch': 'Chinese',
      'por': 'Portugese'
  };

  var choiceMessage = 'You have chosen "' + languages[choice] + '".';

  document.getElementById('choiceDisplay').innerHTML = choiceMessage;
}

Body of popup.html:

<form name="f" onsubmit="sendChoiceAndClose()">
<fieldset><legend>Chinese/Portuguese</legend>

<p><input type="radio" name="lang" value="ch" checked>Chinese</p>
<p><input type="radio" name="lang" value="por">Portuguese</p>
<p><input type="submit" /></p>

</fieldset>
</form>

JavaScript of popup.html:

function sendChoiceAndClose() {
  var form = document.forms['f'];
  var choice = form.lang.value;
  opener.setLang(choice);

  this.close();
}

Here is a overview of the result:

enter image description here

So to give a short explanation, a JavaScript function defined in the opener window is called from the pop-up window to send back the choice information.

Be careful though, if the JavaScript called contains blocking code (for instance alert(...)), the pop-up will be closed only after the blocking code has finished (i.e. after the alert window has been closed for instance).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...