I am not the greatest on javascript and was hoping someone could give me some help. I am trying to pass to the parent form action field from the child. The line I have in the child now which is not working is:
window.parent.document.forms['stopForm'].action = "command_stop_15264.cgi";

I've tried a few variations, but neither worked.
window.parent.document.forms['stopForm'].form.action = "command_stop_15264.cgi"; window.parent.document.forms['stopForm'].form.action.value = "command_stop_15264.cgi";

Dani AI

Generated

: quick diagnostic and a safe pattern that fills the gap between the two suggestions already posted by .

First, two common reasons your assignment fails: the child may be a popup (use opener) or an embedded frame (use parent), and browser same-origin policy will block any DOM access if the pages are on different origins. Also confirm the form is reachable by name or id in the parent DOM (a form with only an id will not always be found by the forms collection).

A quick test (run in the child console) to see whether you can reach the other window and whether same-origin allows access:

try {
  var host = window.opener || (window.parent !== window ? window.parent : null);
  if (!host) throw new Error('no parent/opener');
  // this read will throw if cross-origin
  void host.location.hostname;
  console.log('OK: can access parent/opener');
} catch (err) {
  console.error('Cannot access parent/opener:', err);
}

If that succeeds, this pattern will find the form by name or id and update its action safely:

try {
  var host = window.opener || (window.parent !== window ? window.parent : null);
  var form = host && host.document && host.document.querySelector("form[name='stopForm'], form#stopForm");
  if (form) {
    form.setAttribute('action', '/cgi-bin/command_stop_15264.cgi');
    // form.submit(); // uncomment if you want to submit immediately
  } else {
    console.warn('stopForm not found in parent/opener');
  }
} catch (e) {
  console.error('Cannot reach parent/opener:', e);
}

If access is blocked by cross-origin, use postMessage: have the parent listen for a message that carries the new action and let the parent change its own DOM. Also ensure the parent form actually has a matching name or id, call this after the parent DOM is ready, and prefer absolute paths if relative resolution is producing unexpected URLs.

window.opener.document.forms['stopForm'].action

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.