canadiancreed
canadiancreed

Reputation: 1984

Javascript window.open does nothing, produces no error

So I've been asked to have it where a new window is produced every time a user loads a page. So I've created the following code located in the tag...

<SCRIPT TYPE="text/javascript">
            function popup(mylink, windowname) {
                if (! window.focus) return true;

                var href;

                if (typeof(mylink) == 'string') {
                    href = mylink;
                } else {
                    href = mylink.href;
                    window.open(href, windowname, 'width=400,height=200');
                }

                return false;
            }
        </SCRIPT>

and the onLoad function to create the popup on page load

<body onLoad="popup('http://yahoo.com', 'ad')">

However, nothing happens. No tab is created, no new window is created, and when viewing through firebug, no error is produced. Attempted this on chrome, firefox and IE9 with the same results, with none of the browsers complaining they've blocked a popup window. Am I missing something incredibly simple here?

Upvotes: 0

Views: 1680

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1073968

If mylink is a string (and it is, in your example), you never call window.open:

if (typeof(mylink) == 'string') {
    // Code takes this path
    href = mylink;
} else {
    // Not this one
    href = mylink.href;
    window.open(href, windowname, 'width=400,height=200');
}

Having said that: If you fix that, you will get the warning/error from pop-up blockers. Browsers prevent sites from opening pop-ups except in direct response to a user action, like a click. Sites opening pop-ups on page load is one of the reasons for that.


Side note: typeof is an operator, not a function. No need for parens around the operand.

Upvotes: 3

Related Questions