Search code examples
javascripthtmldomasp-classicdhtml

Take Postcode to New Field in New Window


I have a read only field on a html field which has a name of _Dataaddr_postcode I now need to capture this data and pass it into a new window which loads another file (proxcomp.asp) and use the data in a field on this page the field has an ID of inpAddr.

I have this code so far

<script type="text/javascript">
var pcodeStart = document.getElementbyId("_Dataaddr_postcode");
var newWindow;
function makeNewWindow( ) {
if (!newWindow || newWindow.closed) {
newWindow = window.open("../hpwprox/proxcomp.asp","sub","status=0,title=0,height=600,width=800");
setTimeout("writeToWindow( )", 50);
} else if (newWindow.focus) {
newWindow.focus( );
}
}
</script>
<input type="button" value="Create New Window" onclick="makeNewWindow();" />

Can someone tell me how to achieve this with some sample code?

Thanks

Justin.


Solution

  • Passing just that one field as a form input to the server-side script:

    var genForm = document.createElement("form");
    genForm.target = "sub";
    genForm.method = "get"; // or "post" if appropriate
    genForm.action = "../hpwprox/proxcomp.asp";
    
    var genInput = document.createElement("input");
    genInput.type = "hidden";
    genInput.name = "inpAddr";
    genInput.value = pcodeStart.value;
    genForm.appendChild(genInput);
    
    document.body.appendChild(genForm);
    
    if(!newWindow || newWindow.closed) {
        window.open("", "sub", "status=0,title=0,height=600,width=800");
    } else if(newWindow.focus) {
        newWindow.focus();
    }
    
    genForm.submit();
    

    If you wish to use client-side code to set a textbox in the pop-up rather than server-side code, you need to do it from the pop-up window to avoid the delay you would add otherwise and the page's load time from "racing" each other. In JavaScript, global variables are properties of the window object they exist inside of, and window.opener gives the window that opened this one. Note that because of the same-origin policy, the two windows need to have the same protocol, hostname, and port number in their URLs.

    // Using the variable referring to the text box:
    document.getElementById('inpAddr').value = window.opener.pcodeStart.value;
    
    // Or even using getElementById directly:
    document.getElementById('inpAddr').value = window.opener.document.getElementById('inpAddr').value
    

    You can omit the window. part of window.opener if you want to, provided that you are using no variable called opener.