PageSourceSearch

https://www.fc-gifu.com/wp/wp-content/themes/fc-gifu/common/js/gaddress-finder.js

js fc-gifu.com collected 2026-09-24 19:09:54 UTC 3,577 bytes, 116 lines download raw bytes

1/**
2 *  GAddress Finder - Google日本語入力APIを使って、郵便番号から住所を検索
3 *  @see       http://0-oo.net/sbox/javascript/gaddress-finder
4 *  @version   0.2.0
5 *  @copyright 2011-2013 [email protected]
6 *  @license   http://0-oo.net/pryn/MIT_license.txt (The MIT license)
7 *
8 *  See also
9 *  @see http://www.google.com/intl/ja/ime/cgiapi.html
10 */
11var GAddressFinder = {};
12/**
13 *  郵便番号から住所を検索する
14 *  callbackに引数で渡されるのは [<住所>, <都道府県>, <都道府県以外の住所>] の配列
15 *  @param	String	zip	郵便番号(フォーマットは"000-0000" or "0000000")
16 *  @param	Function	callback	検索結果を渡す関数
17 *  @return	String	検索実行時は半角にしてハイフンを入れた郵便番号、そうでなければnull
18 */
19GAddressFinder.call = function (zip, callback) {
20  if (zip) {
21    zip = this._toHan(zip); //全角の場合は半角にする
22  }
23
24  var matches = zip.match(/^(\d{3})(\d{4})$/);
25
26  if (matches) {
27    //ハイフンが無い場合は入れる
28    zip = matches[1] + '-' + matches[2];
29  }
30
31  if (!zip.match(/^\d{3}-\d{4}$/)) {
32    return null;
33  }
34
35  this._userCallback = callback;
36
37  var url = '//www.google.com/transliterate'; //httpsにも対応
38  url += '?langpair=ja-Hira|ja&jsonp=GAddressFinder.callback&text=' + zip;
39
40  var script = document.createElement('script');
41  script.src = url;
42  script.type = 'text/javascript';
43  document.body.appendChild(script);
44
45  return zip;
46};
47/**
48 *  郵便番号から検索した住所を入力欄にセットする
49 *  @param	String	zipId	郵便番号を入力する要素のid属性
50 *  @param	String	prefId	住所をセットする要素のid属性
51 *  @param	String	otherId	(optional) 都道府県とその他を分ける場合のその他のid属性
52 *  @return	Boolean	検索を実行したかどうか
53 */
54GAddressFinder.set = function (zipId, prefId, otherId) {
55  var zip = document.getElementById(zipId);
56
57  var result = this.call(zip.value, function (address) {
58    if (!address) {
59      alert('該当する住所がありませんでした');
60      return;
61    }
62
63    var elm = document.getElementById(prefId); //住所欄 or 都道府県欄
64
65    if (!otherId) {
66      //テキストボックス1つの場合
67      elm.value = address[0];
68      return;
69    }
70
71    if (elm.tagName == 'INPUT') {
72      elm.value = address[1];
73    } else {
74      for (var i = 0, len = elm.length; i < len; i++) {
75        if (elm.options[i].text == address[1]) {
76          elm.options[i].selected = true;
77          break;
78        }
79      }
80    }
81
82    document.getElementById(otherId).value = address[2];
83  });
84
85  if (result) {
86    zip.value = result;
87    return true;
88  } else {
89    alert('正しい郵便番号を入力してください');
90    return false;
91  }
92};
93/**
94 *  Google日本語入力APIからJSONPで呼ばれる
95 *  @param	Array	results	検索結果
96 */
97GAddressFinder.callback = function (results) {
98  this._userCallback(results[0][1][0].match(/^(.{2,3}?(?:都|道|府|県))(.*)$/));
99};
100/**
101 *  半角に変換
102 *  @param	String	before
103 *  @retrun	String
104 */
105GAddressFinder._toHan = function (before) {
106  var zen = '0123456789-ー'; //長音符号も変換してあげる
107  var han = '0123456789--';
108  var after = '';
109
110  for (var i = 0, len = before.length; i < len; i++) {
111    var c = before.charAt(i);
112    after += han.charAt(zen.indexOf(c)) || c;
113  }
114
115  return after;
116};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.