1

I am using a child window to perform some action and trying to pass some parameters if error occurs. The problem is that, the parameters in the URL keep on stacking, I want to remove all parameters of the parent window via child window so that it only shows the parameters added newly.

Code I'm using right now:

window.opener.location.reload(false);

window.opener.location.href.replace(/&status=[^&;]*/,'');
window.opener.location.href.replace(/&error_code=[^&;]*/,'');

if (window.ammendError) {
    window.opener.location.href += "&status=error&error_code=" + window.ammendErrorCode;
}

window.close();

In this code, its adding the status and error_code parameter. But its not removing the previous parameters from URL.

Zain Sohail
  • 464
  • 5
  • 22
  • `replace` **returns** the new string. (It can't modify the string you call it on; strings are immutable.) Separately: Strongly recommend only modifying `window.opener.location.href` *once*. `window.opener.location.href = window.opener.location.href.replace(/&(?:status|error_code)=[^&;]*/,'') + (window.ammendError ? "&status=error&error_code=" + window.ammendErrorCode : "");` – T.J. Crowder Feb 23 '18 at 07:51
  • Side note: "amend" only has one m in it. :-) – T.J. Crowder Feb 23 '18 at 07:52
  • Thankyou for you comment. But `window.opener.location.href = window.opener.location.href.replace(/&(?:status|error_code)=[^&;]*/,'') + (window.ammendError ? "&status=error&error_code=" + window.ammendErrorCode : "");` this isn't working either. Its still stacking my parameters – Zain Sohail Feb 23 '18 at 08:04
  • If I `console.log` the `window.opener.location.href` it gives me this `?order-id=10376&error_code=482&status=error&error_code=482&status=error&error_code=482` excluding the domain. – Zain Sohail Feb 23 '18 at 08:13
  • My bad, when combining the two `replace` calls, I should have added the `g` (global) flag to the regex. Example: https://jsfiddle.net/jzm8aypz/2/ – T.J. Crowder Feb 23 '18 at 08:18

0 Answers0