Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new URL() -- Node.js vs Browser

Documentation claims that the WHATWG URL API should behave the same in Node.js and in a Browser, but here is a counterexample:

console.log(new URL('http://exa mple.com').origin)

Run this in the browser (or here on stack overflow) and you get http://exa%20mple.com

Run this in Node.js (or here on RunKit) https://runkit.com/embed/cfo21jyrrvxq and you get TypeError: Invalid URL

The browser's output is incorrect, that is not a valid URL. Is there a way to make the browser behave like Node.js? I would prefer a long-term standards-compliant solution to just installing another package.

like image 311
howrad Avatar asked Oct 16 '25 07:10

howrad


1 Answers

This is a problem with Chrome's non-standard implementation of the URL interface. You could attempt to use Node's URL implementation in the browser.

like image 125
woodendoors7 Avatar answered Oct 18 '25 23:10

woodendoors7



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!