Obter o parâmetro url jquery Ou Como Obter Valores de String de Consulta em js

Tenho visto muitos exemplos de jQuery onde o tamanho e nome do parâmetro são desconhecidos. Minha url só vai ter 1 string:

http://example.com?sent=yes

Eu só quero detectar:

  1. O "enviado" existe?
  2. É igual a " sim"?
Solução

Melhor solução aqui.

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = window.location.search.substring(1),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
        }
    }
};

E é assim que você pode usar essa função assumindo que o URL é, http://dummy.com/?technology=jquery&blog=jquerybyexample.

var tech = getUrlParameter('technology');
var blog = getUrlParameter('blog');
Comentários (33)

Espero que isto ajude.

 <script type="text/javascript">
   function getParameters() {
     var searchString = window.location.search.substring(1),
       params = searchString.split("&"),
       hash = {};

     if (searchString == "") return {};
     for (var i = 0; i < params.length; i++) {
       var val = params[i].split("=");
       hash[unescape(val[0])] = unescape(val[1]);
     }

     return hash;
   }

    $(window).load(function() {
      var param = getParameters();
      if (typeof param.sent !== "undefined") {
        // Do something.
      }
    });
</script>
Comentários (0)

Experimente esta **promoção de trabalho*** http://jsfiddle.net/xy7cX/

API:

*inArray` : http://api.jquery.com/jQuery.inArray/

Isto deve ajudar :)

**código***

var url = "http://myurl.com?sent=yes"

var pieces = url.split("?");
alert(pieces[1] + " ===== " + $.inArray("sent=yes", pieces));
Comentários (1)