回答編集履歴

1

window\.nameを使わないソースに変更

2016/03/09 13:52

投稿

notable
notable

スコア415

test CHANGED
@@ -10,58 +10,78 @@
10
10
 
11
11
 
12
12
 
13
- ```JavaScript
13
+ 【2016/03/09更新】
14
14
 
15
- $(function() {
15
+ ソースを変更しました。よく考えると開いたウィンドウの参照があるので、`window.name`が取得できなくても直接`window.location`を変更すれば目的とする動作になります。
16
16
 
17
- var windowRef;
17
+ ブラウザ依存などあるかもしれませんが、FirefoxとChromeでは動作しました。
18
18
 
19
- var windowOpen = function(href){
20
19
 
21
- var windowName = '_new';
22
20
 
23
- if(typeof windowRef != 'undefined') {
21
+ ```HTML
24
22
 
25
- if(windowRef.name != ''){
23
+ <!doctype html>
26
24
 
27
- windowName = windowRef.name;
25
+ <html>
28
26
 
29
- }
27
+ <head>
30
28
 
31
- }
29
+ <title>sample</title>
32
30
 
33
- windowRef = window.open(href, windowName);
31
+ <script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
34
32
 
35
- return windowRef;
33
+ <script>
36
34
 
37
- };
35
+ $(function() {
38
36
 
39
-
37
+ var windowRef = null;
40
38
 
41
- $('#link1').click(function(e) {
39
+ var windowOpen = function(href) {
42
40
 
43
- e.preventDefault();
41
+ if(windowRef && !windowRef.closed) {
44
42
 
45
- windowOpen('index.html');
43
+ windowRef.location = href;
46
44
 
47
- });
45
+ } else {
48
46
 
49
- $('#link2').click(function(e) {
47
+ windowRef = window.open(href);
50
48
 
51
- e.preventDefault();
49
+ }
52
50
 
53
- windowOpen('index2.html');
51
+ return windowRef;
54
52
 
55
- });
53
+ };
56
54
 
57
- $('#link3').click(function(e) {
58
55
 
59
- e.preventDefault();
60
56
 
61
- windowOpen('index3.html');
57
+ var targetWindowOpen = function(e) {
62
58
 
63
- });
59
+ e.preventDefault();
64
60
 
61
+ windowOpen($(this).attr('href'));
62
+
63
+ };
64
+
65
+
66
+
67
+ $('.targetWindowOpen').click(targetWindowOpen);
68
+
65
- });
69
+ });
70
+
71
+ </script>
72
+
73
+ </head>
74
+
75
+ <body>
76
+
77
+ <a href="test.html" class="targetWindowOpen">link1</a><br>
78
+
79
+ <a href="https://ja-jp.facebook.com/" class="targetWindowOpen">link2</a><br>
80
+
81
+ <a href="https://www.google.co.jp/" class="targetWindowOpen">link3</a><br>
82
+
83
+ </body>
84
+
85
+ </html>
66
86
 
67
87
  ```