I have a link wich opens a the second page with links wich call a javascript function.

I have some problem with javascript.


i am two aspx page..

in my first aspx page contain the second aspx page name..

i am clicking the second page link the second page is opened..

but

i wrote the javascript finction in second page..

in this the function does not execute..

how to solve the problem.......

please help me..

first Page name.......form1

second page name....form2

form1.aspx

<input type="text" name="stationFrom" size="16" value="" onblur="return openStationFrom();" >

function openStationFrom()
{


window.open("../enquiry/form2.aspx?" & mstrSession & "&stnName="+escape(document.form1.stationFrom.value)+"&formName=Form1&fieldName=stationFrom&leftWidth=0","","dependent=yes,width=350,height=375,screenX=200,screenY=300,titlebar=no,scrollbars=auto,maximize=no");
}

form2.aspx

<input type="hidden" name="formName" value='Form1'>


<input type="hidden" name="fieldName" value='stationFrom'>

<select name="stationNameCode" size=5 >

<option value='voter id(vt)'</option>

<option value='passport(PO)"option>

</select>
<input type="button" value="GO" onclick=" return setStationField()">

function setStationField()
{
if(document.form2.stationNameCode.selectedIndex<0)
{
alert("Please Select The Station From The List");
return false;
}

formName = document.form2.formName.value;
fieldName = document.form2.fieldName.value;
window.opener.document.forms[formName][fieldName].value = document.form2.stationNameCode[document.form2.stationNameCode.selectedIndex].value;
window.close();
}

this is my used source code..

my problem is..

i am click form2.aspx in GO button ..

i want to click the go button the form2 stationNameCode.selected index value go to form1.aspx to stationFrom text box..

i want to pass form2.selected index to form1.aspx

please help me.. how to do....

Dani AI

Generated

Summary: has a parent page that opens a second page but cannot edit the second page's JavaScript, and needs the selected value on that second page to end up back in the parent. As noted, the normal approach is for the child to write into the opener (see Window.opener), but that requires changing the child. With that constraint there are only a few practical options depending on whether both pages share the same origin.

If the child can be changed: have the child call window.opener or send a message with postMessage (cross-origin safe). See Window.postMessage for the recommended cross-origin pattern.

If the child cannot be changed but both pages are same-origin: the parent can keep the window reference returned by window.open and poll the child window's DOM for the selection. Polling must handle the child closing and cross-origin exceptions. Example parent-side pattern:

var child = window.open('form2.aspx', 'pick', 'width=350,height=375');
var poll = setInterval(function() {
  if (!child || child.closed) { clearInterval(poll); return; }
  try {
    var sel = child.document.querySelector('select[name="stationNameCode"]');
    if (sel && sel.selectedIndex >= 0) {
      document.querySelector('input[name="stationFrom"]').value = sel.value;
      clearInterval(poll);
      child.close();
    }
  } catch (err) {
    // cross-origin or other access error
    clearInterval(poll);
    console.error('Cannot access child DOM:', err);
  }
}, 200);

If the child is cross-origin and cannot be modified: direct DOM access or opener calls are blocked by the same-origin policy (see Same-origin policy). Workarounds are to host an intermediate page that is controllable (which can embed or proxy the unmodifiable page and add a small script), or to ask the owner of the child page to add a postMessage call. Troubleshooting notes: verify the opened URL actually contains the select (view source in the browser), check popup blockers and console errors, and confirm form/field names match what the parent code expects.

Recommended Answers

All 3 Replies

You can access the parent window or the window which opened the new window using the global reference 'opener'.

Keep one hidden field each for the data which you want to pass from the new page to the original page and using the reference of the original page (opener), manipulate the fields of the parent window.

Something like this:

<!-- Parent (ee.html) -->
<html>
<head>
    <script>
    function go()
    {    
        window.open("dd.html");
    }
    </script>
</head>
<body>
<form name="frm">    
    <input type="text" value="Hello" id="txt" name="txt" />
    <input type="button" name="btn" id="btn" value="Button" onclick="go();" />
</form>
</body>
</html>
<!-- Child (dd.html) -->
<html>
<head>
    <script>
    function go()
    {
        alert(opener.document.frm.txt.value);
    }
    function change()
    {
        opener.document.frm.txt.value = "This is changed text";
        this.close();
        opener.focus();
    }
    </script>
</head>
<body onload="go();">
    <form>
        <input type="button" value="Change Value in Parent" id="btn" name="btn" onclick="change();"/>
    </form>
</body>
</html>

Thnaks s.o.s~..

thanks for your kind reply....

I am working this code...working properly...

but the issue is i am not change the javascript code in second code..

because the second page is ".html" page ....

the second page javascript does not change..

i am sorry already i have not informed...

but i am not modified second page everything

please how to do..

please help me......

so in my first page i click the one lint the second page window.open() is open..

so i am not chage the seocond page..

please reply...

You have to change the Javascript code in the second page otherwise your purpose won't be achieved. Why can't you change the code in the second page?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.