I have preview like logic where you can preview changes in a HTML page via a window.open() and be able to log in etc but the issue is I want to ensure that the new windows local storage is clear on only the window so a login is required every time a new preview window is open.
Because there is data which is required to maintain the session in the portal where am executing the code to open the new window - if I run localStorage.clear(); it will mean a login will be required in the portal and the window every time that window is opened - which is what I do not want to do, I only want to ensure that is only required for the newly opened window.
I have looked at various ways to handle this here and I have had no joy thus far.
Effectively what am looking to do is the following:
- Open window.
- Clear local storage.
- Reload the window.
An example of some code I have as is:
return setInterval(() => dispatch(getPreviewStatus(requestData)).then(res => {
if (res[dataPreview.propertyNames.status] === dataPreview.status.completed) {
const windowFeatures = 'location=yes,scrollbars=yes,status=yes';
let window = window.open("www.testing.com", '_blank', windowFeatures);
let preview = window.open(URL, '_blank', windowFeatures);
let script = document.createElement('script');
script.textContent = 'alert(\'JS is running\');';
preview.document.head.appendChild(script);
}
}), 5000);
Is it possible to accomplish this via window.open - I did try the top solution here: Running Javascript in new window.open but with no luck - this is what ave tried script.textContent = 'localStorage.clear(); location.reload();';
Many Thanks!
EDIT 1: I am opening the window in the same domain its being called from.
EDIT 2: I am opening the window and can execute the alert - but if try to clear localStorage.clear();
it clears the page that opened the window and the location.reload();
doesn't reload the window.