question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Construction using window.location.href as base throws Invalid URL

See original GitHub issue

When attempting to construct a URL using a relative path and a base argument matching the current document’s window.location.href, an error TypeError: Invalid URL is thrown.

Environment: IE10 Steps to reproduce:

new URL("/my/path", window.location.href)

(in my test my path was http://timelord.local:4200/candidates;location=martin_pl;date=2019-12-24)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lifaon74commented, Jan 15, 2020

I know, the polyfill doesn’t aim to be perfect BUT to be compact. Originally, the base parameter should only contains the origin. Nothing more. The spec evolved and now the base’s path minus last part must be prepend. Because of this evolution, IE cannot support all cases ! If you really need the full implementation: https://github.com/lifaon74/whatwg-url

But you could simply use something like:

const base = new URL(window.location.href);
const path = base.pathname.split('/').slice(0, -1);
const url = new URL(path.join('/') + '/relative/path', base.origin);

Somehow, this could be patched in this polyfill. For this you could do a pull request.

0reactions
lifaon74commented, Jan 16, 2020

Your welcome. I’ll close this issue. If you have any question you can re-open it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

window.location.href doesn't redirect - Stack Overflow
It will run the code in the function and then submit the page instead of redirect. In this case change the type tag...
Read more >
Location href Property - W3Schools
The location.href property sets or returns the entire URL of the current page. Syntax. Return the href property: location.href.
Read more >
URL() - Web APIs - MDN Web Docs - Mozilla
The URL() constructor returns a newly created URL object representing the URL defined by the parameters.
Read more >
How to Check if a JavaScript String is a Valid URL
In JavaScript, you may need to use a URL in the anchor tags or buttons to link the user to another webpage. This...
Read more >
URL objects - The Modern JavaScript Tutorial
So technically we don't have to use URL . But sometimes it can be really helpful. Creating a URL. The syntax to create...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found