/    Sign up×
Community /Pin to ProfileBookmark

Can we modify a field in the new window from parent window?

I am opening a new window from a javascript of in page.I want to set a form field of the new window to some default value via the javascript in parent window. Can any body suggest some method to do this?

What i exactly want is something like a link which runs some javascript to open a new window like..
var mywin = window.open(‘google.com’,’win’);

apply some more javascript so that the search field of google page should be prefilled with my given string “Search for this”.

Thanx in advance ?

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@CharlesDec 05.2006 — Crossing windows is possible, if the user hasn't disable pop-ups, but this you cannot do. JavaScript has a built in security feature that will not let you monkey with somebody else's page.
Copy linkTweet thisAlerts:
@ZnupiDec 05.2006 — mywin.getElementsByName("q")[0].value = "Search for this";
Copy linkTweet thisAlerts:
@ritukaauthorDec 05.2006 — znupi this doesn't work i have tried it already .i think charles is right ?
Copy linkTweet thisAlerts:
@ZnupiDec 05.2006 — Yeah might be... just like with Ajax, you can probably only control pages from your own domain... Didn't know that, thanks for the info, Charles.
Copy linkTweet thisAlerts:
@KorDec 05.2006 — mywin.[COLOR=Red]document[/COLOR].getElementsByName("q")[0].value = "Search for this";

But it works only if the pop up opens a page from the same domain, as Charles said. You can't do that with Google.
Copy linkTweet thisAlerts:
@KorDec 05.2006 — Yeah might be... just like with Ajax, you can probably only control pages from your own domain... Didn't know that, thanks for the info, Charles.[/QUOTE]
You can reach pages from other domain as well (read data) using AJAX, but in combination with a server-side application (php, asp, java...).
Copy linkTweet thisAlerts:
@ZnupiDec 05.2006 — But... why don't you want to go ahead and make the search and not have the user click "Google Search" ...? Let's say you have an input in which the user types the keywords to search for, and this input has the id "searchInput" and name "q". So this would be the HTML :
[code=html]
<form action="http://google.com/search" onSubmit="openWin(); return false;">
<input id="searchInput" name="q"> <input type="submit" value="Search">
</form>
[/code]

And the javascript:
[code=php]
function openWin() {
keyWords = document.getElementById('searchInput').value;
window.open("http://google.com/search?q=" + keyWords);
}
[/code]

That should work... but I'm asking if anyone knows, can there be a target="_blank" to a form object...?

EDIT: Yes, Kor, I am aware of reaching other domains via server-side languages, and sorry for omitting document in my syntax, I rarely work between windows :p
Copy linkTweet thisAlerts:
@ritukaauthorDec 05.2006 — znupi dat was only an example.I know google search string can be appended in url also.

i was trying something like this link as bookmarklet clicking this link will fill the below given form with a string. I am not being domain specific here.am i?

<a href= "javascript: var auto={ fillerup: function() {document.getElementById('qr_message').value='Rituka has entered something automatically';} };auto.fillerup();">This link fills the below given reply form</a>


Its working fine but when i tried opening some other window thru this and then changing the value of field it doesnt work.
Copy linkTweet thisAlerts:
@KorDec 05.2006 — But you can send a query to the google:
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/javascript"&gt;
function searchW(id){
var q=document.getElementById(id).value
window.open('http://www.google.ro/search?hl=en&amp;q='+q,'newwin')
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;
&lt;input type="text" id="txt"&gt;&amp;nbsp;&lt;input type="button" value="Search Google" onclick="searchW('txt')"&gt;
&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@ZnupiDec 05.2006 — Its working fine but when i tried opening some other window thru this and then changing the value of field it doesnt work.[/QUOTE]
Are you sure it's on your own domain? (I'm talking about the new page opened) How exactly are you trying to do this? First of all, you should have a global variable for the new window... Second of all, doing this:
[code=php]
var newWin;
function openAndFill() {
newWin = window.open("popup.html");
newWin.document.getElementById('myInput').value="blah blah";
}
[/code]

will probably not work, since when you call newWin.document.get... the new window will not be finished loading. So you must attach an onload event to it. Probably like this:
[code=php]
var newWin;
function openAndFill() {
newWin = window.open("popup.html");
if (newWin.addEventListener) newWin.addEventListener("load", fillerup, false); //W3C
else if (newWin.attachEvent) newWin.attachEvent("onload", fillerup); // IE
}
function fillerup() {
newWin.document.getElementById('myInput').value="blah blah";
}
[/code]

That, in theory, should work, if it doesn't, let us know.
Copy linkTweet thisAlerts:
@ritukaauthorDec 06.2006 — No this will not serve my purpose even if it works. Actually what i m trying is ...

Please follow following steps to understand my approach:

1) go to http://www.webdeveloper.com/forum/showthread.php?p=677526#post677526

2) try copy and pasting this given string in you browser

javascript: var auto={ fillerup: function() {document.getElementById('qr_message').value='Rituka has entered something automatically';} };auto.fillerup();

3) now click go...

u will see that it fills the reply form with 'Rituka has entered something'...

its ok but if i want this script to change to something that when u paste it in the browser and click go it automatically forward to http://www.webdeveloper.com/forum/showthread.php?p=677526#post677526 and fill the form. After so much discussion i think its not possible ?.

Anyways thanx to all of you for your precious time and if you think it could be possible some other way please let me know.
Copy linkTweet thisAlerts:
@ZnupiDec 07.2006 — It's impossible unless it is on your own domain. If it is on your own domain, there are two methods of achieveing it:

1) Javascript, I described the method in my previous post.

2) Using a server-side language and passing what the user has entered by GET or POST.
×

Success!

Help @rituka spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...