Home > Mobile >  How can I get the data I want from an url address
How can I get the data I want from an url address

Time:08-19

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

CodePudding user response:

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
  • Related