Open a URL in background in service portal

Neha79
ServiceNow Employee
ServiceNow Employee

HI,

 

In Service portal, When user clicks on Ok in the alert popup, i would like to open a URL in the background and not redirect the user to the url. (the user should still be on the current window). Is this possible? If so, can you please suggest how this can be done?

I've tried window.focus() but it din't work

 

Thanks!

1 REPLY 1

Tushar
Kilo Sage
Kilo Sage

Hi @Neha79 

 

I think it is not that straightforward due to browser security restrictions.

Maybe you can try using using a hidden iframe or making an asynchronous request (e.g., with XMLHttpRequest or Fetch API) to load the content without affecting the main user interface.

 

something like this -

function openUrlInBackground(url) {
    var iframe = document.createElement('iframe');
    iframe.style.display = 'none';
    iframe.src=url;
    document.body.appendChild(iframe);

    // maybe  remove the iframe after it has loaded (or after a timeout)
    iframe.onload = function() {
        document.body.removeChild(iframe);
    };
}

// Example usage
openUrlInBackground('https://google.com');

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar