获取客户端公网IP

<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
<script type="text/javascript">
document.write(returnCitySN["cip"]+','+returnCitySN["cname"])
</script>

获取客户端内网IP(IE不支持)

/**
* Get the user IP throught the webkitRTCPeerConnection
* @param onNewIP {Function} listener function to expose the IP locally
* @return undefined
*/
function getUserIP(onNewIP) { // onNewIp - your listener function for new IPs
//compatibility for firefox and chrome
var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new myPeerConnection({
iceServers: []
}),
noop = function() {},
localIPs = {},
ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
key; function iterateIP(ip) {
if (!localIPs[ip]) onNewIP(ip);
localIPs[ip] = true;
} //create a bogus data channel
pc.createDataChannel(""); // create offer and set local description
pc.createOffer().then(function(sdp) {
sdp.sdp.split('\n').forEach(function(line) {
if (line.indexOf('candidate') < 0) return;
line.match(ipRegex).forEach(iterateIP);
}); pc.setLocalDescription(sdp, noop, noop);
}).catch(function(reason) {
// An error occurred, so handle the failure to connect
}); //listen for candidate events
pc.onicecandidate = function(ice) {
if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
};
} // Usage getUserIP(function(ip){
alert("Got IP! :" + ip);
});

原文链接:https://ourcodeworld.com/articles/read/257/how-to-get-the-client-ip-address-with-javascript-only

js获取客户端IP的更多相关文章

  1. JS获取客户端IP地址、MAC和主机名七种方法

    一.使用JS获取客户端IP的几个方法方法一(只针对IE且客户端的IE允许AcitiveX运行,通过平台:XP,SERVER03,2000).获取客户端IP代码:<HTML><HEAD ...

  2. JS获取客户端IP地址、MAC和主机名【转】

    JS获取客户端IP地址.MAC和主机名 引用:JS获取客户端IP地址.MAC和主机名的7个方法汇总 利用搜狐接口 <html> <head> <meta http-equ ...

  3. JS获取客户端IP地址、MAC和主机名的7个方法汇总

    今天在搞JS(javascript)获取客户端IP的小程序,上网搜了下,好多在现在的系统和浏览器中的都无效,很无奈,在Chrome.FireFox中很少搞到直接利用ActiveX获取IP等的JS脚本. ...

  4. nginx反向代理node.js获取客户端IP

    使用Nginx做node.js程序的反向代理,会有这么一个问题:在程序中获取的客户端IP永远是127.0.0.1 如果想要拿到真实的客户端IP改怎么办呢? 一.首先配置Nginx的反向代理 proxy ...

  5. 浅谈js获取客户端IP

    JS前端获取客户端IP的方法基本都是通过三方接口: 常用的方法1: <script src="http://pv.sohu.com/cityjson?ie=utf-8"> ...

  6. js获取客户端IP及地理位置

    php获取方法: 1.<?php 2.function get_ip_place(){ 3.$ip=file_get_contents("http://fw.qq.com/ipaddr ...

  7. (效果五)js获取客户端ip地址及浏览器信息

    在前端开发的时候,有时候为了测试需要得到访问客户的ip地址.虽说是后端来做的,但是我们前端也可以完成. 先说下获取用户ip地址,包括像ipv4,ipv6,掩码等内容,但是大部分都要根据浏览器的支持情况 ...

  8. js获取客户端ip地址

    <script type="text/javascript" src="http://www.coding123.net/getip.ashx?js=1" ...

  9. 利用js获取客户端ip的方法

    1. 通过script标签引入url 比如如下代码: <script type="text/javascript" src="http://pv.sohu.com/ ...

随机推荐

  1. mysql基础sql进阶

    回顾前面的基础命令语句 修改数据表 添加字段: alter table 表名 add 字段名 列类型[not null|null][primary key][unique][auto_incremen ...

  2. 201871010131-张兴盼《面向对象程序设计(java)》第十六周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  3. Git安装(一)

    1. 下载Git 官网地址:https://git-scm.com/downloads 这里选择下载Git-2.23.0-64-bit.exe 2. 安装 安装完成

  4. 【未完成】【Mybatis】字段由字符+数字变量组成,但要对变量进行计算

    ??????????????????????

  5. .NET开发中 springMVC+NHibernate注入失败的几个常见错误

    1.spring程序集没引用,这个一定要引用,还有就是如果有Redis,还需引用ServiceStack 2.webConfig没配置对,这个没对一般会报错 3.也许Global.asax文件没引入全 ...

  6. [LeetCode] 126. Word Ladder II 词语阶梯之二

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  7. [LeetCode] 79. Word Search 词语搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  8. PowerShell常用命令及美化(现代化的CMD)

    PowerShell可谓现代终端,是微软用来替代古老的CMD的. PowerShell拥有面向对象的思想,非常方便. 常用命令 下载文件(此处以install.ps1文件为例) $client = n ...

  9. Mac修改hosts方法

    总有各种各样的原因需要修改hosts文件,那么就来简介下怎么修改.terminal中打开hosts: sudo vim /private/etc/hosts 打开文件后I开启插入模式,在最后一行添加你 ...

  10. Nacos集成Spring Cloud Gateway 基础使用

    项目结构 项目 端口 描述 nacos-provider 8000 服务 nacos-getway 8001 网关 nacos-provider项目依赖 <dependencies> &l ...