Posted By

sergiazow on 09/06/12


Tagged

get javascript php function


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

sergiazow
candicom


Function to simulate PHP $_GET in javascript


 / Published in: JavaScript
 

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.

  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  

You need to login to post a comment.