# IPアドレスの取得について

**URL:** <https://community.cybozu.dev/t/topic/5084>\
**Category:** kintone 開発相談\
**Created:** [2021 年 7 月 13 日午前 5:18 UTC](https://community.cybozu.dev/t/topic/5084 "2021-07-13T05:18:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account2601](https://avatars.discourse-cdn.com/v4/letter/l/9e8a1a/32.png) [@Legacy\_Account2601](https://community.cybozu.dev/u/Legacy_Account2601)\
**Post date:** [2021 年 7 月 13 日午前 5:18 UTC](https://community.cybozu.dev/t/topic/5084/1 "2021-07-13T05:18:01Z")

</div>

&nbsp;

お世話になっております

IPアドレスをアプリへのアクセスの記録として保存しようと考えております

&nbsp;

IPアドレスの取得方法をネットの記事を参考に作ってみたのですが

JQueryで記載しております

&nbsp;

1と２のwindow alertでは表示されるのですが、３では表示されません

関数の外の変数に値を渡すにはどのようにすればよろしいでしょうか

&nbsp;

&nbsp;

また、JQueryなどのライブラリはKintoneシステム管理で設定すれば全アプリで使用できるのでしょうか

&nbsp;

宜しくお願い致します

&nbsp;

&nbsp;

&nbsp;

&nbsp;

(function($) {  
“use strict”;  
kintone.events.on(‘app.record.index.show’, function(event) {

&nbsp;

var IP\_Address;

$.get(“[https://ipinfo.io](https://ipinfo.io)”, function(res) {

//1  
window.alert(res.ip);

IP\_Address = res.ip;

//2

window.alert(IP\_Address);

&nbsp;

}, “jsonp”);

//3  
window.alert(IP\_Address);

});  
})(jQuery);

---

<div class="post-metadata">

**Author:** ![Legacy\_Account980](https://avatars.discourse-cdn.com/v4/letter/l/a183cd/32.png) [@Legacy\_Account980](https://community.cybozu.dev/u/Legacy_Account980)\
**Post date:** [2021 年 7 月 19 日午前 1:18 UTC](https://community.cybozu.dev/t/topic/5084/2 "2021-07-19T01:18:27Z")

</div>

こんにちは！

ipアドレスを取得している間に、プログラム自体は進んでしまって先に3が実行されているのだと思います。

私がjQueryあんまり分からないので書き方はわからないですが、  
「非同期処理」というキーワードも一緒に検索されてみるとなにか見つかるのではないかと思います。  
JavaScriptだとthenメソッドチェーン使ったり、await/asyncを使ったりすると大丈夫なやつです。

それと、kintoneシステム管理でjQueryなどライブラリを設定しておけば全アプリで使えます。

&nbsp;

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1187](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@Legacy\_Account1187](https://community.cybozu.dev/u/Legacy_Account1187)\
**Post date:** [2021 年 7 月 19 日午前 2:18 UTC](https://community.cybozu.dev/t/topic/5084/3 "2021-07-19T02:18:44Z")

</div>

juridonさんご指摘の通り、JSの非同期処理のため、先に3が実行されてしまっている状況です。

おそらく「undefined」が最初に表示されているのではないでしょうか。

&nbsp;

jQueryを使用せずとも以下の方法で簡単に記述することが出来るので参考にしてみてください。

```
(() => {
"use strict";
kintone.events.on('app.record.index.show', async (event) => {
const response = await kintone.proxy('https://ipinfo.io/json','GET',{},{})
const ipAddress = JSON.parse(response[0]).ip;
window.alert(ipAddress);
});
})();

```

&nbsp;
