4

By using document.referrer we will get all the reference of URL in JavaScript, such as the following:

http://localhost/testwordpress/wp-admin/admin.php?page=thesis-options&upgraded=true

From this output how can we extract the query string part only:

?page=thesis-options&upgraded=true

Is there any method in JavaScript?

3 Answers 3

5

To get the query string from document.referrer, you can use the split() method:

var qs = document.referrer.split('?')[1];

if (typeof qs !== 'undefined') {
    // qs contains the query string.
    // this would be "page=thesis-options&upgraded=true" in your case.
}
else {
    // there was no query string in document.referrer.
}
Sign up to request clarification or add additional context in comments.

1 Comment

This works in theory, but in practice, pages like Google prohibit the browser to include the parameters if they respect the referrer policy.
1

If you are just looking to get the values from the query string I use the following function:

function getQuerystring(key)
{
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

Simply pass in the key you are looking for and get the value back. IE: getQueryString('upgraded') would return true

2 Comments

Note that you may want to change window.location.href with document.referrer.
@Daniel Vassallo: That is a good point. window.location.href will only have what the user sees in it's browser. If you are looking for referrer specific info, it may be better to use document.referrer.
-1

There are some functions around to do that. See this for example.

1 Comment

Answer needs some code, not only a link.

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.