0

Suppose I am loading a JavaScript file like this:

<script type="text/javascript" src="http://foo.com/script.js?id=120#foo"></script>

Is it possible to read GET or hash parameter passing through this?

I am currently doing this using PHP (with headers) like:

<script type="text/javascript" src="http://foo.com/script.php?id=120"></script>
JJJ
  • 32,902
  • 20
  • 89
  • 102
  • 1
    possible duplicate of [How do I get query string value from script path?](http://stackoverflow.com/questions/4716612/how-do-i-get-query-string-value-from-script-path) – JJJ Jun 19 '14 at 13:48
  • I guess I'm not sure which component in your application is supposed to read the hash. Are you asking if the JavaScript code can read the `id` parameter or whether the PHP code can read it? – Mario J Vargas Jun 19 '14 at 13:50

1 Answers1

0

Look here, and replace the window.loaction with your src string. How to retrieve GET parameters from javascript?

function getSearchParameters() {
      var prmstr = window.location.search.substr(1);
      return prmstr != null && prmstr != "" ? transformToAssocArray(prmstr) : {};
}

function transformToAssocArray( prmstr ) {
    var params = {};
    var prmarr = prmstr.split("&");
    for ( var i = 0; i < prmarr.length; i++) {
        var tmparr = prmarr[i].split("=");
        params[tmparr[0]] = tmparr[1];
    }
    return params;
}

var params = getSearchParameters();

to get the string:

var str = $('script').attr('src')

or without jQuery:

document.querySelectorAll('script')[0].src
Community
  • 1
  • 1
lukkysam
  • 188
  • 9