0

I have a url like /accounts/?status=done

So on load of the document i want to get the current url and GET data like status=done

so below is my code which can able to get the current url, but not the GET data

<script>
  $(window).load(function() {
                             //alert("window load occurred!");
                             var pathname = window.location.pathname;
                     console.log(pathname);
                     $.get(pathname,function(data){ console.log(data) });
                    });
</script>

So how to get the current url and GET data like ?status=done in jquery on loading of page ?

Edit

So i have edited according to the link provided here as below and still its unable to print the query string parameters

<script>
            function getParameterByName(name) {
                console.log(name+"......................");
                          name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
                          var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
                          results = regex.exec(location.search);
                          return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
                            }

  $(window).load(function() {
                             //alert("window load occurred!");
                             var pathname = window.location.pathname;
                         console.log(pathname+"hurayyyyy");
                     console.log(pathname);
                     var res = getParameterByName(window.location.pathname)
                 console.log(res+"oooppppppppppppssssssssssss");
                    });


</script>

The results was just like below

enter image description here

Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313

3 Answers3

0

Asign the value through server side language on javascript variable and then use that variable value in your javascript code.

here is PHP server side language code:-

var get_status = "<?php echo $_GET['status']; ?>";
    if(get_status== 'done'){
    //here is your code
    }
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Sumit
  • 698
  • 4
  • 11
  • Mark code in your answers properly, please. There is a button that looks like `{}` when you write an answer. Select a code and press that button. – Ondrej Janacek Feb 01 '14 at 14:02
0

How can i get the current URL of the page in jquery

var URL = $(location).attr('href');

Now the variable URL contains the current address of your Browser location.

Mike Clark
  • 1,860
  • 14
  • 21
0

You could also try something like this:

<script>
    $(window).load(function() {
        var URL = $(location).attr('href');
        var GET_ARR = URL.split('/');
        if($.inArray('?status=done', GET_ARR)){
            //do something
        }                      
    });
</script>
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
crisu
  • 150
  • 4