php - How to get variable from url for .js file? -
example: suppose current page url(window.location.href) http://example.com/page.html html page source code is...
<html><head></head><body> <script src="http://example.com/script.js?user=ankit&ptid=18"></script> </body></html>
now need use 'src' variables in script.js , script file script.js should return
var a="ankit" var b="18"
can use echo $_get in php?
found here. if you're using jquery, should helpful.
function geturlparameter(name) { return decodeuri( (regexp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1] ); }
this javascript function return value in url of parameter pass it. in case, call with
var = geturlparameter("user"); var b = geturlparameter("ptid");
edit: misinterpreted original version of question asking getting parameters .html page being loaded. tested solution, , not work within .js file itself. however, if declare variables in .js file, , place in onload event, removing var
in front of a
, b
, should assign variables correctly.
Comments
Post a Comment