Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts

    $_GET 100% in Javascript

    Hey all i have spent about 2 hours today getting this working cross browser and working properly.

    This is a 100% Implementation of php $_GET and works perfectly in JavaScript

    And when i say 100% i mean it, none of this key=val it works with array[]=>val

    E.G if you have the url hello.html?page=2&filters[]=minPrice&filters[]=maxPrice&minPrice=100&maxPrice=1000

    and as you can see there is an array inside this GET information filters[]
    it will setup $_GET['filters'][0] = minPrice and $_GET['filters'][1] = maxPrice

    So you want to see the code here it is::
    PHP Code:
    var $_GET = {};
    var 
    undefined;
    function 
    readURL() {
      var 
    tLoc ""tPairs "";
      var 
    tGet = [];
      var 
    arrays = [];
      var 
    foundGet = -1;
      
    tLoc window.location "";
      
    foundGet tLoc.indexOf('?');
      if (
    foundGet > -1) {
        
    tLoc tLoc.substring(foundGet 1tLoc.length);
        
    tPairs tLoc.split('&');
        for (var 
    0tPairs.lengthi++) {
          
    tGet tPairs[i].split('=');
          
    foundArray tGet[0].indexOf('[');
          if(
    foundArray > -1){
              
    foundArrayEnd tGet[0].indexOf(']')
            if(
    foundArrayEnd > -1){
              
    old tGet[0];
              if (
    arrays[tGet[0]] === undefined){
                
    arrays[tGet[0]] = 0;
                
    $_GET[tGet[0].replace("[]","")] = {};
              }else{
                
    arrays[tGet[0].replace("[]","")] = arrays[tGet[0]] + 1;
              }
              
    $_GET[tGet[0].replace("[]","")][arrays[tGet[0]]] = decodeURIComponent(tGet[1].replace(/+/g,' '));
            }
          }else
              
    $_GET[tGet[0]] = decodeURIComponent(tGet[1].replace(/+/g,' '));
          }
        }
      }
    readURL(); 
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    What's the use? As long as you don't use a Data Base I might rise a legitimate question: why to submit data? To where? To whom?

    If you need to send data to DB, you need a server-side solution. If you don't ... well, if you don't, why bother to submit data?

    Do I miss something?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    Quote Originally Posted by Kor View Post
    What's the use? As long as you don't use a Data Base I might rise a legitimate question: why to submit data? To where? To whom?

    If you need to send data to DB, you need a server-side solution. If you don't ... well, if you don't, why bother to submit data?

    Do I miss something?
    This works with URLS on a massive scale i have built it as i have a search that allows users to enable filters and disable filters now i was not doing URL rebuilding a redirecting for every filter they applied,

    So JavaScript can $_GET so it can loop though them add them to a new URL string and allows you to filter the $_GET to how you want it, once the site that uses it is live i will show you an example

    My Data is used in both php and JavaScript form the URL so i need both but i have an Array in the GET so i needed to build a parser for it
    Last edited by barkermn01; 12-04-2009 at 04:03 PM.
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •