Take Postcode to New Field in New Window

安稳与你 提交于 2019-12-08 14:21:53

问题


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.


回答1:


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.




回答2:


Maybe doing this:

newWindow.document.getElementById('inpAddr').value = pcodeStart;

Or from the open window:

document.getElementById('inpAddr').value = opener.document.getElementbyId("_Dataaddr_postcode").value;

Please read this great article!



来源:https://stackoverflow.com/questions/4556242/take-postcode-to-new-field-in-new-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!