ARTLUNG LAB Share

Created February 4, 2004

Giving Users A Preference Widget for Opening New Windows

What brought that on?

I put up a new professional site for myself at joecrawford.com and decided since I wanted people to visit several of those links, and stay on the page, I would target them to "_blank", spawning new windows. I thought this was kind of a good thing, despite it being a no no I know about -- one person suggested I look at #2 of The Top Ten New Mistakes of Web Design, 1999: Opening New Browser Windows. But I'm obstinate, and knew that I liked it, and so did many of my visitors.

Rather than deciding one way or another, because for me, and for some of my users, I wanted the option -- I decided to give the users a preference setting.

Here's the php page that sets the cookie: I call it "windowcookie.php" and put it at the site root.

<?php

$cookie_life 
time() + 31536000;

// did we set a parameter? set the setting
if ($_GET['new_window'] == "1") {
    
setcookie("new_window""1"$cookie_life);
} else if (
$_GET['new_window'] == "0") {
    
setcookie("new_window""0"$cookie_life);
} else {
    
setcookie("new_window""0"$cookie_life);
}

// bounce us back from whence we came
header('Location: ' $_SERVER['HTTP_REFERER']);

?>

I put this on the header of the page:

<?php
/**
 * If they want new windows, give it to them!
 * php version 7.2
 *
 * @category PHP
 * @package  User_Preference_For_New_Windows
 * @author   Joe Crawford <joe@artlung.com>
 * @license  GPL 2.0+ - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * @version  GIT: $Id$
 * @link     https://artlung.com/
 * @since    2024-12-03
 */
if ($_COOKIE['new_window'] == "1") {
    
?>
    <base target="_blank"/><?php
} else {
    
// otherwise, default to the same window
    
?>
    <base target="_self"/><?php
}
?>

And this is the PHP code in-line on the page to let people see and change the setting:

<p>
    Your link preference:
    <?php
    
/**
     * If they want new windows, give it to them!
     * php version 7.2
     *
     * @category PHP
     * @package  User_Preference_For_New_Windows
     * @author   Joe Crawford <joe@artlung.com>
     * @license  GPL 2.0+ - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
     * @version  GIT: $Id$
     * @link     https://artlung.com/
     * @since    2024-12-03
     */

    
if ($_COOKIE['new_window'] == "1") {
        
?><a href="/windowcookie.php?new_window=0"
             onmouseover="if(document.getElementById)this.innerHTML='change this setting';"
             onmouseout="if(document.getElementById)this.innerHTML='links open in new windows';"
             target="_self">links open in new windows</a><?php
    
} else {
        
// otherwise, default to the same window
        
?><a href="/windowcookie.php?new_window=1"
             onmouseover="if(document.getElementById)this.innerHTML='change this setting';"
             onmouseout="if(document.getElementById)this.innerHTML='links open in this window';"
             target="_self">links open in this window</a><?php
    
}
    
?>
</p>

I hope this has been helpful. Send a comment or a suggestion.

- Joe Crawford