1

For example js file include to page:

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

How get value count in javascript file test.js from url /js/example/test.js?count=2321?

P.S.: how get if i do not know position key? i.e. how get value parametr from key?

Leo Loki
  • 2,457
  • 6
  • 24
  • 29

5 Answers5

2

Try something like this.

Loop on <script>, search the script that you want and cut at the parameter ?

$(document).ready(function(){
  var parameters = $("script[src*='test.js']").attr("src").split('?')[1].split('&');
  var count="Not found";
  for(var i=0;i<parameters.length;i++)
  {
    var param=parameters[i].split('=');
        if(param[0]=="count")
            count=param[1];
  }

  console.log(count);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="/js/example/test.js?not=2321&over=blabla&one=two&count=1234"></script>
Alexis
  • 5,681
  • 1
  • 27
  • 44
  • it ok if i know name and position key. but how get value from key? i update question. and i would like get key **in** file `test.js` – Leo Loki Jul 01 '16 at 09:30
2

Define a method like below to fetch all key values from a url:

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;
}

this method returns an object array that contains all available Key/Values of the url.

finally you can get the src from any script tag and get all available Key/Values like this:

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

$(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")));
    }
  });
   
});
<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>

You can also use the following method to get a specific key from any url:

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, " "));
}

and get the value of a key like this:

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

$(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")));
});
<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>

Edit (Detect current script): There are many ways to detect the current script tag that you can find HERE, but I think the most accurate way is to define a method in each of your scripts with its name on it and loop over all scripts to find the script you want:

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")));
Community
  • 1
  • 1
hsh
  • 1,825
  • 9
  • 16
  • really change `getParameterByName("count", $("script[src*='test.js']").attr("src")` that instead `$("script[src*='test.js']` was use name js file where execute script? for example if script execute in file test.js than use name this file(test.js) and if script execute in file test2.js than automatically use name this file(test2.js) ? – Leo Loki Jul 01 '16 at 09:54
  • @LeoLoki,you mean,can we detect the name of script file that is running the script and replace it with `$("script[src*='test.js']")`? – hsh Jul 01 '16 at 10:00
  • 1
    @LeoLoki, I updated the answer that may help you to detect the current script – hsh Jul 01 '16 at 10:43
1

Try This

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);
});
Vinie
  • 2,983
  • 1
  • 18
  • 29
0

You can also do it like this:

Give an id to your script:

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

And then:

$(document).ready(function() {
    alert($("#YourId").attr('src').split('=')[1]);
});
kkakkurt
  • 2,790
  • 1
  • 30
  • 33
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, " "));
}
Laxman
  • 1,149
  • 2
  • 11
  • 17
  • not work and `getParameterByName(count);` will be show error, becouse count not exists – Leo Loki Jul 01 '16 at 09:57
  • @LeoLoki , please use 'count' instead of count. I made a mistake in my previous suggestion. – Laxman Jul 01 '16 at 09:58
  • yes, but not work with `window.location.href` it will be work if use `$("script[src*='test.js']").attr("src")` as value parametr url – Leo Loki Jul 01 '16 at 10:05
  • I suggest to give id to this particular script tag. And then pass the src attribute to the getParameterByName as follows – Laxman Jul 01 '16 at 10:11