skip to Main Content

I am trying to rename a new tab I am opening up. This new tab contains an iframe with an image as the source. This is how I am opening the tab, but can’t seem to get document.title to work.

var customTab = window.open();
customTab && customTab.document.title = 'My new tab';//this doesn't work
customTab &&
  customTab.document.write(
    '<iframe src="' +
      myImage +
      '" style="border: none;"></iframe>'
  );

Any ideas would be much appreciated.

2

Answers


  1. If I understand correctly, you should try this:

    var customTab = window.open();
    customTab.onload = function() {
      customTab.document.title = 'My new tab';
      customTab.document.write(
        '<iframe src="' + myImage + '" style="border: none;"></iframe>'
      );
    };

    The issue with setting the document.title in your code is likely due to the fact that the content of the newly opened window is dynamically generated with an iframe, and the document.title might not be accessible immediately after the window.open() call.

    Login or Signup to reply.
  2. replace customTab && customTab.document.title = 'My new tab';

    with if (customTab) customTab.document.title = 'My new tab';

    You can’t assign a value to a variable inside an && expression

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search