[thelist] javascript opening new window question

Keith Gaughan keith at digital-crew.com
Thu Jun 23 13:12:25 CDT 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Brian Cummiskey wrote:

> <a href="images/blah.jpg" onclick="newWindow('images/blah.jpg'); return
> false;">Thumbnail image</a>
> 
> JS enabled will get the pop-up.
> non, will get the image on the same page.

Better still:

<script type="text/javascript">
function newWindow(url)
{
    // Blah...
    return false;
}
</script>

...

<a href="images/blah.jpg"
    onclick="return newWindow(this.href)">Thumbnail image</a>

Though the best way would be to remove the onclick attribute and move
all that stuff out of the page completely.

Which brings me on to...

<tip title="Pop up windows that don't suck">
How not to open a popup window:

    <a href="javascript:popup('blah.html')">Open Popup</a>

Better:

    <a href="blah.html"
        onclick="return popup('blah.html')">Open Popup</a>

Even better still:

    <a href="blah.html"
        onclick="return popup(this.href)">Open Popup</a>

Best:

    <!--
        Handler installed on all anchors with class 'popup' in an
        external JS file.
    -->
    <a href="blah.html" class="popup">Open Popup</a>

But we can make it better.

When you're calling window.open() in your handler, think about appending
an extra parameter onto the end of the link to tell the page whether to
display itself as a popup (that is, with navigation and the like
stripped down) or a regular page.

    // ...
    url += (url.indexOf("?") == -1 ? "?" : "&") + "asPopup=true";
    window.open(url, null, features);
    // ...
</tip>

K.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCuvuImSWF0pzlQ04RAvUhAKD4BBIL5isHiTZhmeGXm1N5mLwNJwCdHRGZ
e2ffwI7kCAKqLdmb4SzCqNE=
=kKI/
-----END PGP SIGNATURE-----


More information about the thelist mailing list