OSDN Git Service

Subversion由来のタグを削除
[nucleus-jp/nucleus-jp-ancient.git] / nucleus / javascript / opennew.js
1 /*
2  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) 
3  * Copyright (C) 2002-2012 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  * JavaScript to open non-local links in a new window.
12  *
13  * How to use:
14  *  in the <head>...</head> section of your page, add the following line:
15  *
16  *  <script type="text/javascript" src="nucleus/javascript/opennew.js"></script>
17  *
18  *  Then, add the following to your <body> tag:
19  *
20  *  <body ... onload="setOpenNewWindow(true);">
21  *
22  *  And you're all done.
23  *
24  * Variables that can be overridden if necessary:
25  *      local = something to recognize local URLs (by default, if your page is something like
26  *              http://www.example.com/path/page.html, then local will be automatically set to
27  *              http://www.example.com/path/)
28  *      exception = something to recognize exceptions to the local check. You might need this
29  *                  when you use a 'click-through' type of script (e.g. when
30  *                  http://www.example.com/path/click.php?http://otherpage.com/ would 
31  *                  auto-redirect to otherpage.com and record a click in your logs)
32  *                  In most of the cases, this variable is unneeded and can be left empty
33  *      destinationFrame = name of the destination frame (by default this is "_blank" to spawn a
34  *                         new window for each link clicked)
35  */
36
37
38 var local = document.URL.substring(0,document.URL.lastIndexOf('/'));
39 var exception = "";
40 var destinationFrame = "_blank";
41
42 function setOpenNewWindow(newWin) {
43         if (newWin) {
44                 from = ""; to = destinationFrame;
45         } else {
46                 from = destinationFrame; to = "";
47         }
48
49         for (var i=0; i<=(document.links.length-1); i++) {
50                 if (document.links[i].target == from) {
51
52                         var href = document.links[i].href;
53                         var isLocal = (href.indexOf(local) != -1);
54                         if (isLocal && ((exception=="") || (href.indexOf(exception) != -1)))
55                                 isLocal = false;
56                         if (!isLocal)
57                                 document.links[i].target = to;
58                 }
59         }
60 }