Dynamic target attribute
 
03/10/2005 - 21:25 (Category: Javascript)

Thanks to the following method, visitors can choose where links will be opened (in a new browser window or in the same browser window).
The script consists of two parts: the first one within <head> and </head> tags, and the other one within <body> and </body> tags.
This script is compatible with Internet Explorer, Netscape 6, Mozilla, Safari, Opera.


Part one (within <head> and </head> tags):


<script language="JavaScript" type="text/JavaScript">
<!--
var mioSito="http://www.wowarea.com";
function wo(_checked)
{
  where = _checked?"_blank":"_self";
  for (var i=0; i<=(document.links.length-1); i++)
  {
        if(document.links[i].href.indexOf(mioSito)<0)
          document.links[i].target=where;
  }
}
// -->
</script>




Part two (within <body> and </body> tags):


<form name="form">
<input type="checkbox" name="checktarget" onclick="wo(this.checked)">
Open links in a new window.
</form>


(Notice that you must place the second code snippet shown above at the same location where you want links and checkboxes to be displayed)


The mioSito variable contains your web site's domain name.
The wo(_checked) function inspects all links in the page, analyzing the href tag.
When the examined domain name and that stored into the mioSito variable match, the link will be opened in the same window.
On the contrary, when an external link is reached (i.e.: domain names don't match because they aren't equal) then the checkbox is inspected. In this case the link will be opened in a new window or in the same window, depending on the checkbox status (i.e.: checked status means 'new window' whereas not checked status means 'same window').


Go back

DISCLAIMER:
Any named products on this website are copyright © and/or trademarks ® of their respective companies. INFORMATION ON THIS WEB SITE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. The material included in this site has been compiled from a variety of sources and the site may include technical inaccuracies or typographical errors. Information may be changed or updated without notice. While every effort is made to ensure the correctness of the information provided here, the author can accept no responsibility for damages caused directly or indirectly from the use of such information. The author will not be responsible for damage or loss of data. You are advised to backup data regularly! By entering this site you are agreeing to this and also to never suing the author.

 

Home  About  Mail us!  

Copyright © 1998-2006 Wowarea