Advanced Search

Page 1 of 4 123 ... LastLast
Results 1 to 10 of 32

Thread: Implementation to send variables through the url (with javascript) like PHP $_GET.

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Implementation to send variables through the url (with javascript) like PHP $_GET.

    I remember several times this question has been asked in this forum and I never found an appropriate answer. So today as I was reading a book I thought of sending variables through javascript from one page to the other. This is just an experiment which I thought I should share it with you so that I know what I did wrong and what I need to implement. Here's the script:

    Code:
    <html>
    <head>
    <title>Javascript Implementation to PHP type GET variables.</title>
    <script type="text/javascript">
    var url = String(window.location);
    var index = url.indexOf("?");
    var data = url.substr(index+1);
    var splitted = data.split("=");
    event = splitted[0];
    act = splitted[1];
    </script>
    </head>
    </body>
    <script type="text/javascript">
    if(index == -1){
    document.write("Please Click One of these links: <a href='"+url+"?user=false'>Login as a Guest</a><br /><a href='"+url+"?user=true'>Login as a user</a>");
    } else if(event == "user" && act == "true"){
    document.write("Welcome User.");
    } else {
    document.write("You are a stranger!!!");
    }
    </script>
    </body>
    </html>
    So what do you think guys??

    EDIT: Forgot to mention it still can't catch all the url variables(for e.g this=that&multiple=variables).
    Last edited by shachi; 08-07-2006 at 05:02 PM.

  2. #2
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Here a more user friendly version:

    Code:
    <html>
    <head>
    <title>Javascript Implementation to PHP type GET variables.</title>
    <script type="text/javascript">
    var url = String(window.location);
    var index = url.indexOf("?");
    var data = url.substr(index+1);
    
    var splitted = data.split("=");
    event = splitted[0];
    act = splitted[1];
    </script>
    </head>
    </body>
    <script type="text/javascript">
    if(index == -1){
    document.write(noindex());
    } else if(event == "user" && act == "true"){
    document.write(isuser());
    } else {
    document.write(notuser());
    }
    function noindex(){
    	content = ''
    	content += "Please Click One of these links: <br><a href='"+url+"?user=false'>Guests Click Here</a><br /><a href='"+url+"?user=true'>Users Click Here</a>";
    	return content;
    }
    function notuser(){
    	content = ''
    	content += 'You are a stranger!!!';
    	return content;
    }
    function isuser(){
    	content = ''
    	content += 'Welcome User';
    	return content;
    }
    </script>
    </body>
    </html>

  3. #3
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Is anyone interested?? Hello??

  4. #4
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    I don't really get the point....I mean get variables are useful for form processing, but you don't need to reload the page for that. I can't think of anything that this could be used for.

  5. #5
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It can be useful if you want to implement dynamic/flexible pages with javascript like

    for displaying a form :
    http://somehost.somedomain/somefile....sion?show=form
    to show something else in the same page :
    http://somehost.somedomain/somefile....=somethingelse

    So that's all I made this for. It proved to be useful for me and thought may be someone else could also find this useful.

  6. #6
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Yes but the problem is what if the user has disabled javascript. Your site becomes inaccessible.

  7. #7
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    In that case either the author has to use PHP or go in the traditional way or let his users that js must be enabled before accessing the page.

  8. #8
    Join Date
    Aug 2006
    Location
    America
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by shachi
    Here a more user friendly version:

    Code:
    <html>
    <head>
    <title>Javascript Implementation to PHP type GET variables.</title>
    <script type="text/javascript">
    var url = String(window.location);
    var index = url.indexOf("?");
    var data = url.substr(index+1);
    
    var splitted = data.split("=");
    event = splitted[0];
    act = splitted[1];
    </script>
    </head>
    </body>
    <script type="text/javascript">
    if(index == -1){
    document.write(noindex());
    } else if(event == "user" && act == "true"){
    document.write(isuser());
    } else {
    document.write(notuser());
    }
    function noindex(){
    	content = ''
    	content += "Please Click One of these links: <br><a href='"+url+"?user=false'>Guests Click Here</a><br /><a href='"+url+"?user=true'>Users Click Here</a>";
    	return content;
    }
    function notuser(){
    	content = ''
    	content += 'You are a stranger!!!';
    	return content;
    }
    function isuser(){
    	content = ''
    	content += 'Welcome User';
    	return content;
    }
    </script>
    </body>
    </html>

    I'm sorry, i'm kind of a novice, so could someone please give me a play-by-play of just what this script does? I can use all the help I can get.

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    This is vastly over-complicated.
    Code:
    <script type="text/javascript">
      var _GET = window.location.href.substr(window.location.href.indexOf('?') + 1).split(/&|;/);
      for(var i in _GET)
        if(_GET[i].indexOf('=') === -1)
          _GET[unescape(_GET[i])] = '';
        else _GET[unescape(_GET[i].substr(0, _GET[i].indexOf('=')))] = unescape(_GET[i].substr(_GET[i].indexOf('=') + 1));
    </script>
    Ta-da.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    This function is good enough to get the GET variables:
    Code:
    function get(key_str) {
    	if(window.location.search) {
    		var query = window.location.search.substr(1);
    		var pairs = query.split("&");
    		for(var i = 0; i < pairs.length; i++) {
    			var pair = pairs[i].split("=");
    			if(unescape(pair[0]) == key_str)
    				return unescape(pair[1]);
    		}
    	}
    }
    @Mobulus: This function takes the query string (?this=this&that=that...) from the url and handels it so that you can access evry variable separately.
    -------------------------------
    Aaaawwwww, Twey...
    Last edited by DimX; 08-19-2006 at 09:28 PM.

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
  •