0
  • I tried to use window.open('url', '_blank') with setTimeout() and window.onload() to set the new tab title, but it is not wordked very well, and the new tab will be blocked by some user's browser. So, i tried another way that clicking the 'a' tag to open my new tab, but i can not set the tab title now.
  • Then i tried the way that add a 'iframe' tag , it seemed useful , but it also need using window.open() that maybe blocked by user's brower.
  • I want to name the new tab title with a 'file name' because of the new tab will load the file stream.
            // create the url for blob
            // let pdfUrl = URL.createObjectURL(blob);
            // preview the file with new tab
            // let newTabTitle = gDocName + " - " + gDocSubPath;
            // let win = window.open(pdfUrl, '_blank'); 
            // win.document.title = newTabTitle;

            let link = document.createElement('a');
            link.href = window.URL.createObjectURL(blob);
            link.target = '_blank';
            link.rel = 'noopener noreferrer';
            link.click();
            window.URL.revokeObjectURL(link.href);
            link.remove();

To find a stable and effective solution to set the new tab title...

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • `window.open` might always be blocked by the browser depending on the user's settings and whether you're doing this on an interaction or not. `createElement('a').click()` definitely seems like it would get blocked with standard popup-blocker settings. Without that (with user interaction), the iframe-based solution [here](https://stackoverflow.com/a/24452322/5774952) still seems to work just fine. – Zac Anger May 24 '23 at 02:56
  • I found an answer. I think you can using it. https://stackoverflow.com/questions/27846474/how-to-set-the-title-for-the-new-browser-tab – Pham Duc Quy May 24 '23 at 03:15
  • @ZacAnger, thanks, it is worked! But i still do not understand how to ensure the new tab is not blocked by user's browser, however i can not make every user to change their browser setting. – diudiudiudiudiu May 24 '23 at 06:05
  • @PhamDucQuy, thank you very much for the answer, i have read the question before, but i was not get effect from the solution because it is a little complex for my need. – diudiudiudiudiu May 24 '23 at 06:09
  • @diudiudiudiudiu if you have a user interaction (clicking a button, for example) triggering the window.open, and it's not too deep in the call chain (not in a function called by the button click calling a function calling a function calling window.open, for example), browsers _usually_ won't block it unless the user settings are very aggressive. There's some more discussion over [here](https://stackoverflow.com/questions/2587677/avoid-browser-popup-blockers). – Zac Anger May 24 '23 at 06:09
  • @ZacAnger, i am understand, your example is very nice , thanks a lot sincerely. – diudiudiudiudiu May 24 '23 at 06:26

0 Answers0