Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set title in the window popup

Tags:

javascript

Is it possible to set a title in the window popup?

I have this in javascript:

var popup = window.open('......');
popup.document.title = "my title";

but this does not work..still can't see any title

EDIT: the page popup is displaying is .aspx and it HAS a title tag, but still can't see that on the popup window..

like image 665
Stewie Griffin Avatar asked Sep 21 '11 14:09

Stewie Griffin


2 Answers

Since popup.onload does not seem to work, here is a workaround: http://jsfiddle.net/WJdbk/.

var win = window.open('', 'foo', ''); // open popup

function check() {
    if(win.document) { // if loaded
        win.document.title = "test"; // set title
    } else { // if not loaded yet
        setTimeout(check, 10); // check in another 10ms
    }
}

check(); // start checking
like image 119
pimvdb Avatar answered Sep 18 '22 08:09

pimvdb


I was having problems with the accepted answer until I realized that if you open an existing, slow page that already has a <title> the browser will 1) set your title, then 2) once the document fully loads it will (re)set the popup title with the "normal" value.

So, introducing a reasonable delay (function openPopupWithTitle):

var overridePopupTitle = function(popup, title, delayFinal, delayRepeat) {
    // https://stackoverflow.com/a/7501545/1037948
    // delay writing the title until after it's fully loaded,
    // because the webpage's actual title may take some time to appear
    if(popup.document) setTimeout(function() { popup.document.title = title; }, delayFinal || 1000);
    else setTimeout(function() { overridePopupTitle(popup, title); }, delayRepeat || 100);
}
var openPopupWithTitle = function(url, title, settings, delay) {
    var win = window.open(url, title, settings);
    overridePopupTitle(win, title, delay);
    return win;
}
like image 38
drzaus Avatar answered Sep 21 '22 08:09

drzaus