Open external links optionally in new window

[Total: 0    Average: 0/5]

User can choose to open links outside the site in a new window or in the current window.

Head Code:

<script language="javascript">
<!-- 
var THIS_SITE="http://www.js-examples.com";
function wo(_checked)
{
  where = _checked?"_blank":"_self";
  for (var i=0; i<=(document.links.length-1); i++)
  {
    /*
	 * If THIS_SITE is not in the URL then set the link to
	 * open or not open up a new page as defined by the form checkbox.
	 */
    if(document.links[i].href.indexOf(THIS_SITE)<0)
	  document.links[i].target=where;
  }
}
// -->
</script>

Body Code:

<form name="f">
<input type=checkbox name="c" onclick="wo(this.checked)">Links to other sites open in a new window.
</form>
<list>
<ul>
<li>Visit <a href="http://www.stpauls.co.uk">St. Paul's Cathedral</a>, London</li>
<li>See <a href="http://www.js-examples.com/">another link</a> on this site</li>
<li><a href="http://www.js-examples.com"</a>This site's home page</li>
</ul>
</list>

Times Viewed: 4

Leave a Reply

Your email address will not be published. Required fields are marked *