如何从路径获取查询字符串

时间:2023-02-05 19:39:38

<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:

我想从cdnparameters获取字符串后如何获取字符串,我的意思是我想得到这一部分:

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

2 个解决方案

#1


1  

Use location.search[0]

使用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

[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.

L.E.现在我懂了。如果你想从jQuery中选择它,也许这样的东西可以工作。

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

#2


0  

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

以下代码将返回包含URL参数的JavaScript对象:

// 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:

例如,如果您有URL:

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

This code will return:

此代码将返回:

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

Reference Link

参考链接

#1


1  

Use location.search[0]

使用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

[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.

L.E.现在我懂了。如果你想从jQuery中选择它,也许这样的东西可以工作。

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

#2


0  

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

以下代码将返回包含URL参数的JavaScript对象:

// 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:

例如,如果您有URL:

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

This code will return:

此代码将返回:

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

Reference Link

参考链接