javascript通过访问者ip地址获取ip地址对应的物理地址的API接口:
搜狐IP地址查询接口(默认GBK):http://pv.sohu.com/cityjson
搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8
<script type="text/javascript" src="http://pv.sohu.com/cityjson?ie=utf-8" charset="utf-8"></script> <script type="text/javascript"> var i=returnCitySN; document.write('您的IP<u>' + i['cip'] + '</u>归属地为<u>' + i['cname'] + '</u>'); </script>
<script type="text/javascript" src="http://pv.sohu.com/cityjson?ie=utf-8" charset="utf-8"></script> <script type="text/javascript"> window.onload = function () { document.body.appendChild(document.createTextNode('['+returnCitySN.cname+"]的朋友,您好!")); } </script>
<!doctype html> <html> <head> <meta charset="utf-8"> <title>使用搜狐接口获取客户端IP地址</title> <style> .center { width: 100%; font-size: 18px; text-align: center; } .canvas-center { width: 400px; height: 300px; margin: 0 calc(50% - 200px); } </style> <script> function writeWords(words){ var context = document.getElementById("canvas").getContext("2d"); // 设置字体 context.fontSize = "18px"; // 设置颜色 context.fillStyle = "#000"; // 设置水平对齐方式 context.textAlign = "center"; // 绘制文字(参数:要写的字,x坐标,y坐标) context.fillText(words, 150, 60); } </script> </head> <body> <div class="center" id="ip"></div> <canvas class="canvas-center" id="canvas"></canvas> <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> <script type="text/javascript"> <!-- document.write(returnCitySN["cip"]+','+returnCitySN["cname"]) --> var words = returnCitySN["cname"]+': '+returnCitySN["cip"]; document.getElementById('ip').innerHTML = words; writeWords(words); </script> </body> </html>
还没有评论,来说两句吧...