Function to simulate PHP $_GET in javascript


/ Published in: JavaScript
Save to your folder(s)

Simulate the $_GET array from php in javascript to get url parameters this way:
url= http://localhost/?var1=example
$_GET['var1'] will return "example"

Just copy and paste the code.


Copy this code and paste it in your HTML
  1. var $_GET=new function(){
  2. var fullUrl=window.location.href.split('?');
  3. var urlParams=fullUrl[1].split('&');
  4.  
  5. $_GET=new Array();
  6.  
  7. for(i=0;i<=urlParams.length-1;++i){
  8. var param=urlParams[i].split('=');
  9. var name=param[0];
  10. var value=param[1];
  11. $_GET[name]=value;
  12. }
  13. return $_GET;
  14. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.