Neal Davis
Neal Davis

Reputation: 668

$(window.document).ready(function () { ... } not really ready?

I am using the following window.open (url is same host/domain as current page):

function openWindow() {
   var fswin = window.open(url, msg);
   $(fswin.document).ready(function () {
      setWindowTitle(fswin, msg)   //set window title
   });
}

At times I am getting an error either null / undefined trying to set the title or fs_date value below:

function setWindowTitle(fswin, fs_date) {
    if ((fswin.document.title !== undefined) && (fswin.document.getElementById("fs_date") !== undefined))
    {
        fswin.document.title = fs_date;
        fswin.document.getElementById("fs_date").value = fs_date;
    }
    else //if not loaded yet wait a 50ms then try again
    {
        setTimeout(setWindowTitle, 50); //wait 50ms and check again
    }
}

It is an intermittent error works sometimes not other times; seems to me I can not use setTimeout(setWindowTitle, 50) because it will not pass in the require parameters to setWindow(fswin, fs_date)? Maybe that is the problem it is hitting setTimeout(...) sometimes and therefore does not pass in fswin and fs_date?

What am I doing wrong and how can I fix it?

Upvotes: 1

Views: 109

Answers (1)

Barmar
Barmar

Reputation: 780714

The .ready() method doesn't care what element it's bound to, it always operates on the current document.

Use the load event for other elements.

$(fswin).on("load", function() {
    setWindowTitle(fswin, msg);
});

Upvotes: 2

Related Questions