admin管理员组

文章数量:1122846

I want to set the window open new page's title(this page will open. a pdf file in browser), this is how I do it:

export const handleOpenInBrowserDirect = (projectId: string) => {
  if (projectId) {
    getPreviewUrl(projectId).then((res) => {
      if (ResponseHandler.responseSuccess(res)) {
        var newWindow = window.open(res.result, "_blank");
        setTimeout(() => {
          if (newWindow) {
            newWindow.document.title = "New title";
          }
        }, 300);
      }
    });
  }
};

when I open the new page, the title was changed to New title. But when the new page rendered done. the title changed to preview. Am I missing something? what should I do to change the new page title that opened by window open in react?

I have also tried to listening the load event:

export const handleOpenInBrowserDirect = (projectId: string) => {
  if (projectId) {
    getPreviewUrl(projectId).then((res) => {
      if (ResponseHandler.responseSuccess(res)) {
        var newWindow = window.open(res.result, "_blank");
        if (newWindow) {
          newWindow.addEventListener("load", () => {
            if (newWindow) {
              newWindow.document.title = "New title";
            }
          });
        }
      }
    });
  }
};

I also add this code in the application app component like this:

React.useEffect(() => {
        window.document.title = 'Some Name';
    },[]);

the title will finally replaced by preview when the pdf download and loaded done. It look like the google chrome shows the document's title. Also tried loop listening:

export const handleOpenInBrowserDirect = (projectId: string) => {
  if (projectId) {
    getPreviewUrl(projectId).then((res) => {
      if (ResponseHandler.responseSuccess(res)) {
        var newWindow = window.open(res.result, "_blank");
        const loop = setInterval(function () {
          if (newWindow) {
            if (newWindow.closed) {
              clearInterval(loop);
            } else {
              newWindow.document.title = "winName";
            }
          } else {
            clearInterval(loop);
          }
        }, 1000);
      }
    });
  }
};

本文标签: javascripthow to change the window open page title in reactStack Overflow