[thelist] _blank smackdown 2002, suggestions, then?

.jeff jeff at members.evolt.org
Tue Apr 23 14:59:01 CDT 2002


miriam,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: Miriam Frost
>
> Original
> > > I want my code to be sexy and all XML strict.
>
> .jeff suggested his great evolt.org article
> > User Defined Window Targeting w/ JavaScript
> > http://www.evolt.org/article/thelist/17/16286/index.html
>
> and Julia suggested another thing....
>
> It.
> Doesn't.
> Validate.
> http://makeashorterlink.com/?Z5D712EB
>
> I've tweaked what I could...
> but this stymies me...
>
>    for(a = 0; a < document.links.length; a++)
>                    ^Error: character "<" is the
> first character of a delimiter but occurred as
> data
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

simple.  move the function to an external javascript file.

it's absolute garbage that the validator would complain about javascript.
"<" is a valid operator in javascript.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> and I need a form action....
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

<form
 action="JavaScript://"
 onsubmit="return false">

also, if you want your page to validate you won't be able to explicitly set
targets on off-site links.  so, what you do is check the checkbox onload and
that'll fire off the setTarget function which will set targets on all links.

now, the next thing is that some of your links you're not going to want to
be in new windows, namely those that link to other pages/documents on your
own site.  so, a slight massaging of the function is in order.  basically
you just check whether or not the host property of each link is different
than the host property of the location object.  if it's different, it's an
offsite link and you wanna target it to a new window.

function setTarget(target)
{
  for(a = 0; a < document.links.length; a++)
  {
    if(document.links[a].host != location.host)
      document.links[a].target = target;
  }
}

and since you want to use a checkbox instead of the radio buttons in my
article, you'll need to make a couple of changes.

<form
 action="JavaScript://"
 onsubmit="return false">
<input type="checkbox"
       name="target"
       id="target_blank"
       value="_blank"
       tabindex="3"
       title="Open links in a new window for each link"
       onclick="setTarget((this.checked) ? this.value : '_self')" />
<label for="target_blank"
       accesskey="n"
       title="Open links in a new window for each link"
>new windows</label>
</form>

now, add the following to any document that you want to have this option:

onload="document.forms[0].elements[0].click()"

that should check the box and set the targets on the page for offsite links.

good luck,

.jeff

http://evolt.org/
jeff at members.evolt.org
http://members.evolt.org/jeff/





More information about the thelist mailing list