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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -