简体   繁体   中英

How can I get the data I want from an url address

I had a VUE page that jump from Native side, in order to get the api params I want, Native side passing the data as a url form like

file:///storage/emulated/0/Android/data/com.hon.smarthome/files/w…tail_base/index.html?iotId=xdN3ywsqvJu2tHgY4mOf000000&platform=1#/main

The 1 and xdN3ywsqvJu2tHgY4mOf000000 is the data I want So I use split to get those data

var url = window.location.href; 
var cutNumberSign = url.split('#')[0];
var iotIdPlusPlatform = cutNumberSign.split('?')[1];
var cutIotId = iotIdPlusPlatform.split('iotId=')[1];
var platfrom = cutIotId.split('platform=')[1];
var iotId = cutIotId.split('&')[0];
this.iotId = iotId;
this.platform = platfrom;

It works totally fine from Android side However from iOS I got, the data order was not the same, it was like

index.html?platform=1&iotId=xdN3ywsqvJu2tHgY4mOf000000#/main

so the split will not work, How can I get those data no matter the order is? Thanks

create a new URL object and get the searchParams .

var params = new URL(window.location.href).searchParams;

var platform = params.get('platform')
var iotId = params.get('iotId')
console.log(platform) // 1
console.log(iotId)    // xdN3ywsqvJu2tHgY4mOf000000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM