30-seconds-of-code: [Snippet] getAbsoluteUrl

Created on 16 Sep 2019  路  5Comments  路  Source: 30-seconds/30-seconds-of-code

I found this snippet handy and not included yet.馃槅
Any suggestions ?

const getAbsoluteUrl = (() => {
    let a;
    return relativeUrl => {
        if(!a) { a = document.createElement('a') };
        a.href = url;
        return a.href;
    };
})();
opinions needed

Most helpful comment

Closing due to inactivity...

All 5 comments

Hey @Lucien-X 馃憢

I am not sure I completely understand the functionality of this function.

What does it do differently from accessing window.location.href? I tried to run it on this page and I got a wrong result I think:

window.location.href got me: https://github.com/30-seconds/30-seconds-of-code/issues/1018
while
getAbsoluteUrl() got me: "https://github.com/30-seconds/30-seconds-of-code/issues/undefined" (tldr; the diff is in the last part of the URL).

I might be a bit tired right now (it's soooo late over here) so I don't see the full potential of it. Feel free to give more details on this so we can evaluate this properly 馃憤

Anyways, thanks for the idea and willing to help us! We really appreciate it. 鉂わ笍 :octocat:

@fejes713
Sorry, I forgot to leave the use case here.馃槸

When you have a relative url such as '/30-seconds/30-seconds-of-code',
You can get the absolute url of it depends on the current origin by using

getAbsoluteUrl('/30-seconds/30-seconds-of-code')
// => "https://github.com/30-seconds/30-seconds-of-code"

You can use this way to get rid of the compatibility problem may occur in IE and Opera
MDN Link

// Not supported by IE and Opera
window.location.origin
// Not supported by Opera
window.location.protocol +'//' + window.location.host

Thanks for clarification @Lucien-X 馃憤

@Chalarangelo WDYT?

Closing due to inactivity...

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for any follow-up tasks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Chalarangelo picture Chalarangelo  路  5Comments

konglx90 picture konglx90  路  6Comments

ecwyne picture ecwyne  路  4Comments

larrybotha picture larrybotha  路  3Comments

Greenheart picture Greenheart  路  3Comments