前端使用js如何准确获取当前页面url网址信息

前端使用js如何准确获取当前页面url网址信息,第1张

在WEB开发中,时常会用到javascript来获取当前页面的url网址信息,在这里是我的一些获取url信息的小总结。

下面我们举例一个URL,然后获得它的各个组成部分:>

如果你做的是android 的应用,使用了webview,IMEI还是可以获取的。

使用 addJavascriptInterface(object, "test");

可以用来让web中的js调用本地Java对象,而 获取的IMEI可以在这个本地Java对象中实现。

试试搜一下 android webview js 调用 本地 java,android js java 交互 等关键词。

有时候我们需要判断应用程序当前的方向,可以通过获取设备当前的方向来确定,从下面的定义你可以看到UIInterfaceOrientation的定义是通过UIDeviceOrientation来完成的,有两个概念:

UIDeviceOrientation:硬件设备的方向

UIInterfaceOrientation:应用程序界面的方向

UIDeviceOrientation的定义如下:

[cpp] view plain copy print

typedef enum {

UIDeviceOrientationUnknown,

UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom

UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top

UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right

UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left

UIDeviceOrientationFaceUp, // Device oriented flat, face up

UIDeviceOrientationFaceDown // Device oriented flat, face down

} UIDeviceOrientation;

UIInterfaceOrientation的定义如下:

[cpp] view plain copy print

typedef enum {

UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,

UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,

UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,

UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft

} UIInterfaceOrientation;

可采用下面的方式来判断当前设备的方向:

[cpp] view plain copy print

UIDevice device = [UIDevice currentDevice];

switch (deviceorientation) {

case UIDeviceOrientationUnknown:

NSLog(@"Unknown");

break;

case UIDeviceOrientationFaceUp:

NSLog(@"Device oriented flat, face up");

break;

case UIDeviceOrientationFaceDown:

NSLog(@"Device oriented flat, face down");

break;

case UIDeviceOrientationLandscapeLeft:

NSLog(@"Device oriented horizontally, home button on the right");

break;

case UIDeviceOrientationLandscapeRight:

NSLog(@"Device oriented horizontally, home button on the left");

break;

case UIDeviceOrientationPortrait:

NSLog(@"Device oriented vertically, home button on the bottom");

break;

case UIDeviceOrientationPortraitUpsideDown:

NSLog(@"Device oriented vertically, home button on the top");

break;

default:

NSLog(@"cannot distinguish");

break;

}

或者是:

[cpp] view plain copy print

UIDeviceOrientation deviceOrientation = [UIDevice currentDevice]orientation;

if (UIDeviceOrientationIsLandscape(deviceOrientation))

NSLog(@"The orientation is landscape");

else if(UIDeviceOrientationIsPortrait(deviceOrientation))

NSLog(@"The orientation is portrait");

UIDeviceOrientationIsLandscape的定义如下:

[cpp] view plain copy print

#define UIDeviceOrientationIsLandscape(orientation) ((orientation) == UIDeviceOrientationLandscapeLeft || (orientation) == UIDeviceOrientationLandscapeRight)

UIDeviceOrientationIsPortrait的定义如下:

[cpp] view plain copy print

#define UIDeviceOrientationIsPortrait(orientation) ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown)

有时候我们只需要知道当前设备是水平还是竖直放置,就可以采用第二种方法。

```JavaScript

<!DOCTYPE html>

<html lang="zh-CN">

<head>

    <meta charset="UTF-8">

    <title>Fingerprint2 TEST</title>

    <style>

        body {

            color: #555;

        }

        #info {

            font-size: 12px;

        }

        #control span {

            color: #333;

            margin-left: 10px;

        }

    </style>

</head>

<body>

<div id="info">

    </p>

    <p>纯前端实现的浏览器指纹采集器,通过获取浏览器中所有能获取到的信息(部分通过base64转成String),最后生成出md5,用于该用户在该设备上的唯一标识码,官方宣称准确度高达995%</p>

</div>

<div id="control">

    <button onclick="start()">开始</button>

    <span>userAgent:</span><input type="checkbox" id="userAgent" checked="checked">

    <span>fonts:</span><input type="checkbox" id="fonts" checked="checked">

    <span>fontsFlash:</span><input type="checkbox" id="fontsFlash" checked="checked">

    <span>canvas:</span><input type="checkbox" id="canvas" checked="checked">

    <span>webgl:</span><input type="checkbox" id="webgl" checked="checked">

    <span>audio:</span><input type="checkbox" id="audio" checked="checked">

    <span>enumerateDevices:</span><input type="checkbox" id="enumerateDevices" checked="checked">

</div>

<div id="view">

</div>

<script src=">

WEB这东西,本来就是跨平台的东西,对于不同平台就有不同的浏览器,不同厂家的浏览器又不太一样。尤其这种比较偏的底层接口。就拿开发Android的WEB浏览器常用的WebView,就本身N个版本,接口改了又改。你的网页还要跨平台,不管Android,还是WindowsPhone,还是……,适应于他们的变化有多复杂,自己要三思。

最后,我说我不知道常用的手机浏览器有没有这种接口,你生气不看

网页浏览器又不是你造的,你可以任意改。除非你做那种混合型的App来让用户浏览自己的网站,内嵌WebView,通过JS和App交互,获取你需要的IMEI等等信息。

补充:竟然没人看到真正的答案。单纯Web页面是无法获取IMEI,除非W3C修订JavaScript标准,增加接口,并让浏览器厂商开始支持。目前想获取IMEI,必须是app和Web页面相结合方式。就如当前有个hbuilder开发工具,DCloud开发的。可以调用手机系统API,原理类似于Java反射技术。他们封装的比较好。可以直接通过js调用。当然最后发布的不是真正的纯Web页面,是一个app安装包。这里面的语法不是在任何浏览器都能运行的。但发布的安装包,内部封装好的浏览器是可以支持相应的js方法。

以上就是关于前端使用js如何准确获取当前页面url网址信息全部的内容,包括:前端使用js如何准确获取当前页面url网址信息、ios蓝牙开发怎么快速获取当前手机连接的设备、h5前端app如何获取其他设备wi-fi模块ip等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9688060.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存