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.

4
  • Its possible. Check This out Commented Sep 26, 2011 at 8: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. Commented Sep 26, 2011 at 8:28
  • @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. Commented Sep 26, 2011 at 8: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. Commented Sep 26, 2011 at 8:38

2 Answers 2

-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;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

and what is wrong with this one?
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).
-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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.