1

I want to read out a GET variable from an url using jquery

The domain is displayed like this

http://example.com/p/kT2Rnu35

And the original is :

http://example.com/php/page.php?id=kT2Rnu35

I want to get that id using jQuery from the page with the domain http://example.com/p/kT2Rnu35

I've tried window.location and other functions i found on stack overflow but nothing worked. Is it because i'm changing the url using htaccess ?

2 Answers 2

1

window.location.href will give you the url with parameters

From there just use .split to break it apart

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

Comments

1

const getParameterByName = (name, url) => {
  const regex = new RegExp(`[?&]${name}(=([^&#]*)|&|#|$)`);
  const results = regex.exec(url);
  if (!results) return null;
  if (!results[2]) return '';
  return decodeURIComponent(results[2].replace(/\+/g, ' '));
};

let url = 'https://website.com/abc?id=123';

console.log(getParameterByName('id', url));

// For url like this `http://example.com/p/kT2Rnu35` simply get the last token after `/`

url = 'http://example.com/p/kT2Rnu35';
let id = url.substring(url.lastIndexOf('/') + 1);
console.log(id);

2 Comments

but i want to get the variable directly from the page example.com/p/12345678 not from the original
@nadhirdz Update my answer for that case

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.