OSDN Git Service

b317721d0f4d1257880540db51e8bf00ba3df4a4
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / javascript / opennew.js
1 /*
2  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) 
3  * Copyright (C) 2002-2007 The Nucleus Group
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * (see nucleus/documentation/index.html#license for more info)
10  *
11  * $Id: opennew.js,v 1.5 2007-02-04 06:28:45 kimitake Exp $
12  * $NucleusJP: opennew.js,v 1.4 2006/07/12 07:11:47 kimitake Exp $
13  *
14  * JavaScript to open non-local links in a new window.
15  *
16  * How to use:
17  *  in the <head>...</head> section of your page, add the following line:
18  *
19  *  <script type="text/javascript" src="nucleus/javascript/opennew.js"></script>
20  *
21  *  Then, add the following to your <body> tag:
22  *
23  *  <body ... onload="setOpenNewWindow(true);">
24  *
25  *  And you're all done.
26  *
27  * Variables that can be overridden if necessary:
28  *      local = something to recognize local URLs (by default, if your page is something like
29  *              http://www.example.com/path/page.html, then local will be automatically set to
30  *              http://www.example.com/path/)
31  *      exception = something to recognize exceptions to the local check. You might need this
32  *                  when you use a 'click-through' type of script (e.g. when
33  *                  http://www.example.com/path/click.php?http://otherpage.com/ would 
34  *                  auto-redirect to otherpage.com and record a click in your logs)
35  *                  In most of the cases, this variable is unneeded and can be left empty
36  *      destinationFrame = name of the destination frame (by default this is "_blank" to spawn a
37  *                         new window for each link clicked)
38  */
39
40
41 var local = document.URL.substring(0,document.URL.lastIndexOf('/'));
42 var exception = "";
43 var destinationFrame = "_blank";
44
45 function setOpenNewWindow(newWin) {
46         if (newWin) {
47                 from = ""; to = destinationFrame;
48         } else {
49                 from = destinationFrame; to = "";
50         }
51
52         for (var i=0; i<=(document.links.length-1); i++) {
53                 if (document.links[i].target == from) {
54
55                         var href = document.links[i].href;
56                         var isLocal = (href.indexOf(local) != -1);
57                         if (isLocal && ((exception=="") || (href.indexOf(exception) != -1)))
58                                 isLocal = false;
59                         if (!isLocal)
60                                 document.links[i].target = to;
61                 }
62         }
63 }