ani_css
ani_css

Reputation: 2126

How can I get query string from path

<script src="//files.mywebsite.com/js/main.js?cdn=%2f%2fcdn.path.com%2fweb"></script>

I want to get string after from cdnparameters how can I get string, I mean I want to get this section:

%2f%2fcdn.path.com%2fweb

Upvotes: 2

Views: 97

Answers (2)

ellimist
ellimist

Reputation: 626

Use location.search[0]

In your example, you can get what you want with:

location.search.split("=")[1]

[0] https://www.w3schools.com/jsref/prop_loc_search.asp

L.E. Now I get it. If you want to select that from jQuery, maybe something like this can work.

let value = $('script[src*="//files.mywebsite.com/main.js"]').src.split("=")[1];
console.log(value)

Upvotes: 1

Asif Sharif Shahid
Asif Sharif Shahid

Reputation: 370

The following code will return a JavaScript Object containing the URL parameters:

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
var vars = [], hash;
var hashes=window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
  for(var i = 0; i < hashes.length; i++)
  {
     hash = hashes[i].split('=');
     vars.push(hash[0]);
     vars[hash[0]] = hash[1];
  }
  return vars;
}

For example, if you have the URL:

http://www.example.com/?me=myValue&name2=SomeOtherValue

This code will return:

{
  "me"    : "myValue",
  "name2" : "SomeOtherValue"
}

Reference Link

Upvotes: 0

Related Questions