-2

I want a username from a URL like "example.com/u/AbcUser". I know we can get it by using HTTP GET request, But I want to create a regular latest site

is it possible with Vanilla JS and HTML ??

Dixit Ram
  • 43
  • 6
  • 1
    Simply [parse it](https://stackoverflow.com/q/736513)? – InSync May 11 '23 at 05:48
  • 2
    `url.split('/').pop()` ..? – Teemu May 11 '23 at 05:48
  • BTW i am using Firebase's host services, will it throw an error of no page found ? – Dixit Ram May 11 '23 at 05:51
  • 1
    How can a user login or open a page if a page is not found? If a page is not found, then JS wouldn't start in the first place as it is never requested in that case... – tacoshy May 11 '23 at 05:52
  • @tacoshy yaaa , That Is my concern , I created a profile page then the user will input the URL like ** example.com/profile/AbcUser ** then Firebase will find and go to AbcUser not Profile page right? – Dixit Ram May 11 '23 at 05:55

2 Answers2

1
const url = window.location.pathname
const userName = url.split("/")[2]
Yasin Khan
  • 83
  • 1
  • 10
  • How would this work with an URL like `http://example.com/u/AbcUser` ..? – Teemu May 11 '23 at 09:14
  • window.location.pathname will get the path name after base url "/u/AbcUser" url.split("/") will split the string on places where "/" is present ["", "u", "AbcUser"] now you just use [2] index to get the user name "AbcUser" – Yasin Khan May 11 '23 at 10:17
  • Yes, but OP never stated that they use `location` to get the URL, they say (in a comment) that it is a user input. – Teemu May 11 '23 at 10:19
0
    <h1 id="wel" style="text-align: center; size: 25;"> </h1>
`enter code here` function getUserName(){
    var url = location.href;
    var spliting =url.split("?");

    var splitUser = spliting[1].split("&");

    var userNam= splitUser[0].split("=");


   return document.getElementById('wel').innerHTML = "Welcome :  &nbsp; "+userNam[0];

    }

    getUserName();
DaveL17
  • 1,673
  • 7
  • 24
  • 38
  • this code working good – Samuel Gamil Jul 20 '23 at 14:31
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 23 '23 at 14:04