You forgot to call transferText()
After calling transferText() the text was transferred...
<html>
<head>
<script type="text/javascript">
function init()
{
popupWin = window.open('','popupWin','');
popupWin.document.writeln('<html><head><title>test</title></head><body><form><input type="text" id="popupTextBox"/></form></body></html>');
popupWin.document.close();
popupText = popupWin.document.getElementById("popupTextBox");
parentText = document.getElementById("parentTextBox");
transferText();
}
function transferText()
{
popupText.value = parentText.value
}
</script>
</head>
<body>
<input type="text" id="parentTextBox"/>
<input type="button" onclick="init();"/>
</body>
</html>
Answer from Fabian N. on Stack Overflowhow to pass value to pop up window using javascript function? - Stack Overflow
Pass Variable to a Popup Window using Javascript - Stack Overflow
javascript - How to pass data to parent window from popup window? - Stack Overflow
Javascript Passing value from a popup window to its parent window - Stack Overflow
Videos
Due to security restrictions it is super easy only if the parent window and the popup are from the same domain. If that is the case just use this:
// Store the return of the `open` command in a variable
var newWindow = window.open('http://www.mydomain.com');
// Access it using its variable
newWindow.my_special_setting = "Hello World";
In the child (popup) window, you could access that variable like this:
window.my_special_setting
Was there something specific you wanted to do past that?
In the parent use:
let newWindow = window.open('http://www.example.com');
newWindow["myVar"] = "Hello World"
In the Child use: window["myVar"] to access it.
Assign window.open() to a variable so you can access it's elements.
<form>
<input type="textbox" id="txt" />
<input type="button" id="btn" value="Open window" />
</form>
<script>
document.getElementById('btn').onclick = function(){
var myWindow = window.open();
myWindow.document.body.innerHTML = document.getElementById('txt').value;
};
</script>
Suppose your text field's id is myTextField, whatever you named it. if it has no id, set a id for it. then, in the popup window, you can use JavaScript parent.document.getElementById('myTextField').value to get the value of the textfield.
use window.opener
From Mozilla Developer Network: When a window is opened from another window, it maintains a reference to that first window as window.opener. If the current window has no opener, this method returns NULL.
https://developer.mozilla.org/en-US/docs/Web/API/window.opener
This way you can have on your original window a callback, and you can notify the window it's load and ready, rather than wait a random delay...
you add a function on the original window:
window.popupReady = function (callbackToPopup) {
callbackToPopup(newData);
}
then the popup can tell the parent window it's ready and pass it a callback to update it with data..
and on the popup try something like:
window.dataReady(newData)
{
alert(newData);
}
document.addEventListener("load", function() { window.opener.popupReady (dataReady); }
I didn't test this code, but I would take such a path as this should ensure the popupWindow is ready for you and is along the spirit of JavaScript.
In your onclick attribute you pass '90' to the function, but the function isn't set up to take an argument. So, change the first line of your function like this:
function writeToWindow(data) {
You don't need the global var data; or the local var k = data;, so get rid of them.
And instead of + k + write + data +.
That should do get your data passed.
The window.opener object is what you're looking for, used it from within your popup like so to call the a function of the parent window:
window.opener.yourFunc()
Here is a fun and easy demo that is heavily inspired by this answer to a similar question (but modified for my own purposes to help investigate the most difficult bug of my career).
Create 2 files (in the same directory) as follows:
parent.html
<button type="button" onclick="popup('popup.html', '', 800, 200);">Add My Card</button>
=>
<span id="retrievedData">No data yet.</span>
<script>
function popup(url, title, width, height) {
var left = (screen.width / 2) - (width / 2);
var top = (screen.height / 2) - (height / 2);
var options = '';
options += ',width=' + width;
options += ',height=' + height;
options += ',top=' + top;
options += ',left=' + left;
return window.open(url, title, options);
}
function setData(data) {
console.log(data);
var strData = JSON.stringify(data);
document.getElementById('retrievedData').innerHTML = strData;
var requestBinUrl = 'http://requestb.in/18u87g81';
window.location.href = requestBinUrl + '?data=' + strData;
}
</script>
popup.html
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form id="popupForm" name="f">
<select id="urlField" name="url">
<option>
http://date.jsontest.com/
</option>
<option>
http://time.jsontest.com/
</option>
<option>
http://md5.jsontest.com/?text=HereIsSomeStuff
</option>
</select>
<div><input type="submit" /></div>
</form>
<script>
$('#popupForm').submit(function(e) {
e.preventDefault();
var url = $('#urlField').val();
console.log(url);
$.ajax({
url: url
}).then(function(data) {
console.log(JSON.stringify(data));
window.opener.setData(data);
window.close();
});
});
</script>
I finally figured it out Use window.opener
window.opener.document.getElementById('archiveimages').value = address.src;
When the user select the image from the popup, use the following statement to set the value of the input field. Assuming the id of the input field is 'myInputField'
window.parent.document.getElementById('myInputField').value = imargeUrl;
Bear with me, I've new to Javascript.
I'm trying to create a little 'share' bookmarklet but cannot pass the data from the parent document to the popup's elements. The popup's input fields are blank.
What am I missing?!
Here's my parent script:
var title = document.title;
var url = window.location.href;
var popForm;
function get_elements() {
formObject = popForm.document.forms["link_form"];
formObject.elements["id_title"].value = title;
formObject.elements["id_url"].value = url;
}
function bookmark() {
popForm = window.open("form.html","aWindow","height=500,width=400");
popForm.window.onload=get_elements
}And my child just look slike this:
Title:</br> <input id="id_title" type="text" name="title" maxlength="50" value="" /> </p> Description:</br> <textarea id="id_descrip" rows="10" cols="40" name="descrip"></textarea> </p> Url: <input id="id_url" type="text" name="url" maxlength="100" title="" /> </p> <input type="submit" value="Submit" />
Is the popup serving content from a different domain than the parent? If so, the short answer is you can't.
The long answer is that you can sent the popup's href fragment (i.e. the part after the # in protocol://server/path?query#fragment). If the content in the popup knows to check its fragment for changes, then you can pass data to it.
If it's from the same domain then your code should work, as long as an element with that id exists.
However, the getElementById function returns NULL.
Because popup.html hasn't loaded yet. If you want to interact with content from the document, you'll have to call back later when it has finished loading.
For completely dynamic popups, open them with a blank URL and popupwindow.document.write their content into them. For co-operatively-scripting popups loaded from a separate document, have the child document call its parent when it is ready to be accessed. Or just use in-page pop-up divs which are typically less annoyance, both for you as a coder and for the end user.
If the page in the popup is in the same domain as yours, you can use window.opener to access data of the opener windows
In your current window :
var winopened;
$("#popup").click(function(e) {
var url = $(this).attr('href');
e.preventDefault();
winopened = window.open(url, 'authWindow', "width=800,height=436");
});
if(winopened) {
// call a function in the opened window :
res = winopened.functionInWindow();
}
hope it helped ...
window.open will return your window object.
var myWindow = window.open
Now you can play with myWindow.