admin管理员组

文章数量:1327487

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

<html><head></head><body>
<script src=".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?

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

<html><head></head><body>
<script src="http://example./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?

Share Improve this question edited May 14, 2013 at 13:43 Ankit_Shah55 asked May 14, 2013 at 13:27 Ankit_Shah55Ankit_Shah55 8272 gold badges10 silver badges17 bronze badges 5
  • 4 possible duplicate of How can I get query string values? – Cyril N. Commented May 14, 2013 at 13:29
  • I didn't get it what u said – underscore Commented May 14, 2013 at 13:29
  • see developer.mozilla/en-US/docs/DOM/window.location – Lim H. Commented May 14, 2013 at 13:29
  • possible duplicate of stackoverflow./questions/247483/… – aynber Commented May 14, 2013 at 13:30
  • stackoverflow./questions/2090551/… – Nicholas Decker Commented May 14, 2013 at 13:44
Add a ment  | 

3 Answers 3

Reset to default 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.

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

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./script.js?user=Ankit&ptid=18"></script>
</body></html>

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

HTML :

<script src="http://example./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");

本文标签: phpHow to get variable from url for js fileStack Overflow