Read the original article:How to Parse an URL String
Question
How can I convert a string into a URL and extract query parameters in HarmonyOS?
Short Answer
The native JavaScript new URL() is not supported in HarmonyOS. Instead, use the @ohos.url package to parse URLs and access query parameters.
Applicable Scenarios
When you need to work with URL strings—such as extracting query parameters, host, or pathname—in a HarmonyOS app where standard Web APIs like new URL() are unavailable.
Code Example
let that = url.URL.parseURL('http://username:password@host:8080/directory/file?foo=1&bar=2#fragment');
console.log("hash " + that.hash) // hash #fragment
console.log("host " + that.host) // host host:8080
console.log("hostname " + that.hostname) // hostname host
console.log("href " + that.href) // href http://username:password@host:8080/directory/file?foo=1&bar=2#fragment
console.log("origin " + that.origin) // origin http://host:8080
console.log("password " + that.password) // password password
console.log("pathname " + that.pathname) // pathname /directory/file
console.log("port " + that.port) // port 8080
console.log("protocol " + that.protocol) // protocol http:
console.log("search " + that.search) // search ?foo=1&bar=2
console.log("username " + that.username) // username username
console.log("params: foo " + that.params.get("foo")) // params: foo 1
Key Takeaways
HarmonyOS does not support the native new URL() API, so use the @ohos.url package’s parseURL() method to work with URL parts and query parameters. This ensures compatibility when extracting host, pathname, or query values in HarmonyOS apps.
Reference
https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-url
Top comments (0)