Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript datafields...multiple pages 1

Status
Not open for further replies.

JSMoose

Programmer
Mar 6, 2005
7
US
Hello,

Can JavaScript set the value of fields in a webpage that it opens without modifying the new page.

Example: Page1 opens page2 through a button. I would like to populate a field in page2 without modifying any of page2's code. I can only modify page1.

Thanks,

Moose
 
JSMoose,

You can access the page2 document via its handle, if you have modeless window open in mind. Like this.
[tt]
var owin=window.open("page2")
owin.document.formname.fieldname.value="xyz"
//etc
[/tt]
regards - tsuji
 
tsuji,

Thanks for your reply. With my limited knowledge of JavaScript I tried the code you suggested. However, I probably did not impliment it correctly because it did not work. I have pasted the code below.

Page1 - jstest.htm

<script language="JavaScript"><!--
function newWindow(file,window)
{
var owin=open(file)
owin.document.a.htm.t1.value="xyz"

}
//--></script>

<form name="P1">
<input type="button" value="Open New Window" onClick="newWindow('a.htm','window2')">
</form>


Page2 - a.htm

<form name="P2" >
<input type="text" name="t1" id="t1" >
</form>

Again I would like to populate t1.value from the click event of P1's button.

Thanks,

Moose
 
JSMoose,

[1] It is then translated into this.
[tt]
owin.document.P2.t1.value="xyz"
[/tt]
[2] Make sure the a.htm can be accessed this way, it may not. Supply the full path to it if it is the local testing m/c with file protocol. Something like this.
[tt]
onClick="newWindow('file:///d:\\test\\a.htm','window2')
[/tt]
[3] Avoid using variable name like window. Although you might not cause trouble in this case, it is no good to name a variable.

- tsuji
 
tsuji,

Thanks once again for the help. I implimented your suggestions and the page worked right the first time but ever since it has not ran correctly. I get an error on Page1 after I click the 'Open New Window' button. If I go to the details of the error IE tells me that 'document.P2.t1 is null or not an object'.

I am using IE6.

Any reason why it would work once but not again?

Code being used:

Page1 - jstest.htm

<script language="JavaScript"><!--
function newWin(file)
{
var owin=open(file)
owin.document.P2.t1.value="xyz"

}
//--></script>

<form name="P1">
<input type="button" value="Open New Window" onClick="newWin('a.htm')">
</form>

jstest.htm and a.htm are both in the same directory.

Any more help would be great.

Thanks,
JSMoose

 

You have to wait for the document to have loaded before you can access elements on the page.

Try using a setTimeout with a small delay (say 1 second)... Or combining a setTimeout with setting the onload event handfler for the body of the new document, and then using that to set the value.

Hope this helps,
Dan



The answers you get are only as good as the information you give!

 
JSMoose,

What BRPS said is correctly addressing the issue of timing due to loadtime, but still leave very much to good judgement to chance that the delay is sufficient. Else, you have to implement try-catch(e) loop for full flexibility.

One thing though I want to add here is when you set in a time delay like this,
[tt]
setTimeout('owin.document.P2.t1.value="xyz"',1000);
[/tt]
it will be no good unless you make the handle global scope.
[tt]
//var owin=open(file); //no good any more in scope
owin=open(file); //make it global in scope
[/tt]
- tsuji
 
It works great.

Thanks tsuji and BRPS.

JSMoose
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top