0

Possible Duplicate:
Pass vars to JavaScript via the SRC attribute

May I know how to read get the p value on js file link like filename.js?p=value with local javascript in the js file? Any function to work like the $_GET['p'] in php? Thanks.

Community
  • 1
  • 1
davidlee
  • 5,611
  • 17
  • 56
  • 82
  • @BenLee it is a completely different question. One is pass by variable, this is pass by link's parameter. Pls read the question again Ben. Thanks. – davidlee Sep 26 '11 at 08:34
  • 1
    @davidlee Look again at the non-accepted answers to that question: they show how to get query parameters from the URL used to load the script. – Richard Sep 26 '11 at 08:38
  • Its possible. Check [This](http://www.dominopower.com/issues/issue200004/howto002) out – Anuj Verma Sep 26 '11 at 08:25
  • This just tells you (in a very convoluted manner) how to use `window.location.search` to extract the query string from the current request url. But that is completely different than a query string *to* a javascript file. – Ben Lee Sep 26 '11 at 08:28

2 Answers2

-1

try this:

var tmp = location.href, it;
if (q) it = tmp.substr(q + 1).split('&');
else it = '';
for (var i in it) {
    var t = it[i].split('=');
    if (t[0] == 'p') {
        //do something
        break;
    }
}
Subdigger
  • 2,166
  • 3
  • 20
  • 42
  • and what is wrong with this one? – Subdigger Sep 26 '11 at 08:33
  • Not the one to downvote, but let me explain what is wrong: `window.location` point to the location of the document that *loaded* the current script. OP wants to extract the query string of the script file *itself* (as loaded from the HTML document). – jensgram Sep 26 '11 at 08:54
-1
function _GET( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

This will do the equivalent in javascript.

Neil
  • 5,762
  • 24
  • 36