2

Example: Suppose the current page url(window.location.href) is http://example.com/page.html The html page source code is...

<html><head></head><body>
<script src="http://example.com/script.js?user=Ankit&ptid=18"></script>
</body></html>

Now I need to use 'src' variables in script.js And the script file script.js should return

var a="Ankit"
var b="18"

Can we use something like echo $_GET like in php?

Ankit_Shah55
  • 799
  • 2
  • 9
  • 17

3 Answers3

5

Found this here. If you're using jQuery, this should be helpful.

function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}

This is a javascript function that will return the value in the url of a parameter that you pass to it. In this case, you would call it with

var a = getURLParameter("user");
var b = getURLParameter("ptid");

EDIT: I misinterpreted the original version of your question as asking about getting parameters to the .html page being loaded. I just tested this solution, and it does not work within the .js file itself. However, if you declare your variables in the .js file, and place this in the onLoad event, removing var from in front of a and b, it should assign the variables correctly.

Community
  • 1
  • 1
jonhopkins
  • 3,844
  • 3
  • 27
  • 39
  • I want to get parameter from server side – Ankit_Shah55 Sep 26 '13 at 05:20
  • If by that you mean, in JavaScript, you want to retrieve values from the server, but not from the URL of the page, then you should do some research on Ajax requests. If jQuery is available for this project, it will make your life *much* easier for this. If you have any specific questions once you try to get that working, post a new question on StackOverflow since it's a different topic than this one, and let me know if you want my help. – jonhopkins Sep 26 '13 at 12:41
3

Maybe outdated but a nice piece of code and would exactly do what was asked for in OP

// Extract "GET" parameters from a JS include querystring
function getParams(script_name) {
  // Find all script tags
  var scripts = document.getElementsByTagName("script");

  // Look through them trying to find ourselves
  for(var i=0; i<scripts.length; i++) {
    if(scripts[i].src.indexOf("/" + script_name) > -1) {
      // Get an array of key=value strings of params
      var pa = scripts[i].src.split("?").pop().split("&");

      // Split each key=value into array, the construct js object
      var p = {};
      for(var j=0; j<pa.length; j++) {
        var kv = pa[j].split("=");
        p[kv[0]] = kv[1];
      }
      return p;
    }
  }

  // No scripts match
  return {};
}

Source: James Smith - Extract GET Params from a JavaScript Script Tag

Lahmizzar
  • 487
  • 6
  • 6
1

I know it's an old post, but as I was looking for something like that I came across it. The very simple solution I finally adopted is the following one:

<html><head></head><body>
<script>
 var a = "Ankit";
 var b = 18;
</script>
<script src="http://example.com/script.js?user=Ankit&ptid=18"></script>
</body></html>

If you absolutely want to complicate your life and use Lahmizzar's solution, I would recommend to give an id to your tag script, which avoids a greedy function.

HTML :

<script src="http://example.com/script.js?user=Ankit&ptid=18" id="myScript"></script>

JS :

 function getParams(script_id) {
  var script = document.getElementById(script_id);
  
    if(script) {
      // Get an array of key=value strings of params
      var pa = script.src.split("?").pop().split("&");

      // Split each key=value into array, the construct js object
      var p = {};
      for(var j=0; j<pa.length; j++) {
        var kv = pa[j].split("=");
        p[kv[0]] = kv[1];
      }
      return p;
  }

  // No scripts match
  return {};
}
getParams("myScript");
scraaappy
  • 2,830
  • 2
  • 19
  • 29