1

Hi guys I am trying to take token from url. I am trying this code

     const userURL = document.location;
     const shared_token = userURL.split('/')[4].split('?')[0];
     alert(shared_token)

my url is something like this:

http://www.something.com/earlyAccessPage/%217y%2299%24SYE8FJ7s9oVWix1i6HSC9ega21nXhhOty0UbgrX09jQhssmXMPhXK?fbclid=IwAR2cFvZgJAJsm1zaaRVdb20vzGzZg1qnazTtW-9Bm25DIsiSkdIBEKdjNfo

But it's show error: userURL.split is not a function. Please help me to correct my code.

2
  • 1
    The Document.location read-only property returns a Location object. Try document.location.href or document.URL instead. Read Document.location. Commented Jun 11, 2020 at 7:55
  • i tried document.URL; const shared_token = userURL.split('/')[4].split('?')[0]; alert(shared_token); but gettinh error like "const shared_token = userURL.split('/')[4].split('?')[0]; alert(shared_token)". I need only take token part "%217y%2299%24SYE8FJ7s9oVWix1i6HSC9ega21nXhhOty0UbgrX09jQhssmXMPhXK" Commented Jun 11, 2020 at 10:11

3 Answers 3

2

Use location.pathname - that can be document.location or window.location - either will work for you: Location object

location.pathname.split("/").pop()

returns

%217y%2299%24SYE8FJ7s9oVWix1i6HSC9ega21nXhhOty0UbgrX09jQhssmXMPhXK

Using your code:

const userURL = document.location;
const shared_token = userURL.pathname.split('/').pop(); // take the last part of the  path
alert(shared_token)

NOTE In the example I use new URL() to make a URL from the URL you provided. You just need document.location

// const shared_token= location.pathname.split("/").pop() in your real code
const shared_token= new URL("http://www.something.com/earlyAccessPage/%217y%2299%24SYE8FJ7s9oVWix1i6HSC9ega21nXhhOty0UbgrX09jQhssmXMPhXK?fbclid=IwAR2cFvZgJAJsm1zaaRVdb20vzGzZg1qnazTtW-9Bm25DIsiSkdIBEKdjNfo")
  .pathname
  .split("/")
  .pop()
console.log(shared_token)

To get parameters AFTER the ? use

// const url = location
const url = new URL("http://www.something.com/earlyAccessPage/%217y%2299%24SYE8FJ7s9oVWix1i6HSC9ega21nXhhOty0UbgrX09jQhssmXMPhXK?fbclid=IwAR2cFvZgJAJsm1zaaRVdb20vzGzZg1qnazTtW-9Bm25DIsiSkdIBEKdjNfo")

const usp = new URLSearchParams(url.search)

const fbclid = usp.get("fbclid")

console.log(fbclid)

Sign up to request clarification or add additional context in comments.

9 Comments

From the code snippet provided in the question description, the shared_token is not in the search params
@Hangindev That is a variable name OP gave it. Please run the code
I believe the questioner was trying to get "%217y%2299%24SYE8FJ7s9oVWix1i6HSC9ega21nXhhOty0UbgrX09jQhssmXMPhXK" part but not the "fbclid=IwAR2cFvZgJAJsm1zaaRVdb20vzGzZg1qnazTtW-9Bm25DIsiSkdIBEKdjNfo" part.
can you help me to get only "%217y%2299%24SYE8FJ7s9oVWix1i6HSC9ega21nXhhOty0UbgrX09jQhssmXMPhXK" part?
@VardanHambardzumyan - I am doing exactly that. See update for explanation - Look in the first example location.pathname.split("/").pop()
|
1

this is wrong. document.location returns location object. you are try to split location object. so instead of that try to use const url = document.URL.

ref : Document.URL

 const userURL = document.URL;
 const shared_token = userURL.split('/')[4].split('?')[0];
 alert(shared_token)

Comments

0

You get a Location object back from document.location which already splits up the url for you in handy ways. See the docs for details, but it looks like userURL.search would give you what's after the question mark. So your code could simply to:

// if you want the first value after the '?' you can do this:
const search = document.location.search;
const firstSearch = search ? search.substring(1).split("&")[0] : "";

// if you want the value immediately before the '?' you can do this:
const segments = document.location.pathname.split("/");
const lastSegment = segments[segments.length-1];

You can always use the original userURL.href but see the various parts of Location that are already prepared for you (like host, port, pathname and search).

6 Comments

PS: OP is not looking for the search but what is just before the search
I agree that OP's code was getting the url segment before the '?' but given the variable name "shared_token" I expect they really are wanting what's after the '?'. however I've updated my answer to show getting both values.
const segments = document.location.pathname.split("/"); const lastSegment = segments[segments.length-1]; I used this code but it return only "earlyAccessPage" I need to get only this part "%217y%2299%24SYE8FJ7s9oVWix1i6HSC9ega21nXhhOty0UbgrX09jQhssmXMPhXK"
what does document.location.pathname give you?
first code give nothing, second one gives "earlyAccessPage"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.