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

// file by joe crawford 2003/02/05
//
// windowcookie.php
// how long do the cookies live? a long time
$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: ' $HTTP_REFERER);

?>

I put this on the header of the page:

<?php
    
// if they want new windows, give it to them!
    
if($HTTP_COOKIE_VARS ['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!
    
if($HTTP_COOKIE_VARS ['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