SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Get method?

  1. #1
    SitePoint Zealot diskhub's Avatar
    Join Date
    Jun 2004
    Location
    singapore
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Get method?

    Hi, i currently doing some small html project for my company.
    Due to some restriction, I am only allow to create html... no server, no php.

    I wish to use GET method and pass it to another html page where javascript can keep up and use the value of the variable.

    ac.html?name=john

    How do i retrieve value "john" using html n javascript only?
    Please assist.
    I LOVE PHP!!!

  2. #2
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use location.href to retrieve the current page's URL, then use functions like indexOf and split to chop it into usable pieces.

  3. #3
    SitePoint Zealot diskhub's Avatar
    Join Date
    Jun 2004
    Location
    singapore
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, if i use PHP and i get a variable to be the $_GET['variable'],
    how can i pass this variable into Javascript function?

    I used to assign this variable to some html element such as a hidden form input. Then use Javascript to read the value of that element thru document.getElementById.

    Is this the right way or is there a better way?
    I LOVE PHP!!!

  4. #4
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by diskhub
    ac.html?name=john

    How do i retrieve value "john" using html n javascript only?
    Buddy has your answer. All you need is something like xGetURLArguments.

  5. #5
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I realize this is late but...

    I don't think you need to get as complicated as xGetURLArguments (not saying it's bad to, just saying it's not necessary)...a little DOM snippet accomplishes the same thing:
    Code:
    function johnCheck() {
    			
    if ( document.location.search == "?john" ) {	
    					
    	     johnAction();
    	   }
    	else if ( document.location.search == "?paul" ) {
    	     paulAction();			
               }
    	else
    			return;
    }
    The code is a little hard to format right... sorry about that.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,839
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    window.location.search contains everything from the ? on in the URL. Since the normal format for passing multiple options involves separating them with & the first thing to do would be to run a split('&") on it to create an array of the entries passed. As Each entry consists of a key and a value separated by an = you could then do a split('=') on each entry in the array so as to get the key in the [0] entry of the array and the value in the [1] entry.

    To load all of them into an associative array called "values" you could use:

    Code:
    var values = [];
    var searchterms = window.location.search.split('&");
    for (var i = searchterms.length-1; i >= 0; i--) {
    var tA = searchterms[i].split('=');
    values[ta[0]] = tA[1];
    }
    Now if a value was passed as ?name=john the values['name'] field would contain 'john'.
    Last edited by felgall; Jan 26, 2007 at 04:10.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps it should be noted: The use of document.location has largely been deprecated in favor of window.location.


Bookmarks

Posting Permissions

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