如何从url获取params到javascript或jquery文件?

时间:2016-07-01 09:08:50

标签: javascript jquery

例如js文件包含到页面:

<script src="/js/example/test.js?count=2321"></script>

如何从网址count获取 javascript文件test.js中的/js/example/test.js?count=2321 值?

P.S。:如果我不知道位置键怎么办?即如何从密钥中获取值参数?

5 个答案:

答案 0 :(得分:2)

尝试这样的事情。

循环$regex = '#^[0-9-_,/\s]{4,100}$#'; ,搜索您想要的脚本并剪切参数$regex = '#^[0-9{4,100}-_,/\s]+$#';

<script>
?

答案 1 :(得分:2)

定义如下方法,从网址中获取所有键值:

function getUrlVars(url)
{
   var vars = [], hash;
   if(url == undefined || url.indexOf('?') ==  -1)
     return vars;
   var hashes = url.slice(url.indexOf('?') + 1).split('&');
   for(var i = 0; i < hashes.length; i++)
   {
      hash = hashes[i].split('=');
      vars.push({key : hash[0] , value : hash[1]});
   }
   return vars;
}

此方法返回一个对象数组,其中包含网址的所有可用键/值。

最后,您可以从任何脚本标记获取src并获取所有可用的键/值,如下所示:

$("script").each(function(){
  if($(this).attr("src") != undefined){
    console.log($(this).attr("src") + ":");
    console.log(getUrlVars($(this).attr("src")));
  }
});

&#13;
&#13;
$(document).ready(function(){
  function getUrlVars(url)
  {
   var vars = [], hash;
    if(url == undefined || url.indexOf('?') ==  -1)
      return vars;
   var hashes = url.slice(url.indexOf('?') + 1).split('&');
   for(var i = 0; i < hashes.length; i++)
   {
      hash = hashes[i].split('=');
      vars.push({key : hash[0] , value : hash[1]});
   }
   return vars;
  }
  $("script").each(function(){
    if($(this).attr("src") != undefined){
      console.log($(this).attr("src") + ":");
      console.log(getUrlVars($(this).attr("src")));
    }
  });
   
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="/js/example/test.js?count=2321&customKey=1233&customKey2=sdffds"></script>
<script src="/js/example/test1.js?count2=2321&Key2=sdffds"></script>
<script src="/js/example/test2.js?count2=2321&key1=1233"></script>
&#13;
&#13;
&#13;

您还可以使用以下方法从任何网址获取特定密钥:

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

并获取这样一个键的值:

getParameterByName("count", $("script[src*='test.js']").attr("src"))

&#13;
&#13;
$(document).ready(function(){
  function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
  }
  
   console.log( getParameterByName("count", $("script[src*='test.js']").attr("src")));
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="/js/example/test.js?count=2321"></script>
&#13;
&#13;
&#13;

编辑(检测当前脚本):有很多方法可以检测到您可以找到 HERE 的当前脚本标记,但我认为最准确方法是在每个脚本中定义一个方法,并在其上添加其名称,并遍历所有脚本以找到所需的脚本:

function isMe(scriptElem){
   return scriptElem.getAttribute('src') === "Your Current Script Src";
}

var me = null;
var scripts = document.getElementsByTagName("script")
for (var i = 0; i < scripts.length; ++i) {
   if( isMe(scripts[i])){
     me = scripts[i];
   }
}
console.log( getParameterByName("count", $(me).attr("src")));

答案 2 :(得分:1)

试试这个

function getParameterByName(name,url) 
{
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(url);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

$(document).ready(function(){
    var url = $("script[src*='test.js']").attr("src");
    getParameterByName('count',url);
});

答案 3 :(得分:0)

你也可以这样做:

为您的脚本提供ID:

<script id="YourId" src="/js/example/test.js?count=2321"></script>

然后:

$(document).ready(function() {
    alert($("#YourId").attr('src').split('=')[1]);
});

答案 4 :(得分:0)

var paramCount = getParameterByName('count');
function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}