How to make my JQuery code recognise a parameter in the URL and then choose a line of code

StackOverflow https://stackoverflow.com/questions/13215925

  •  30-07-2021
  •  | 
  •  

Pergunta

In the code below, I want to change the line of code that reads as:

$("#commentload"+ID).prepend(html);

So that if the parameter order=1 is found in the URL, for example, http://my.com?order=1then it should execute.

$("#commentload"+ID).append(html);

Instead of

$("#commentload"+ID).prepend(html);

Hope you understand what I mean.

$(document).ready(function(){
   // Update Status
   $(".update_button").click(function(){
      var updateval = $("#update").val();
      var dataString = 'update='+ updateval;
      if(updateval==''){
         alert("Please Enter Some Text");
      } else {
         $("#flash").show();
         $("#flash").fadeIn(400).html('Loading Update...');
         $.ajax({
            type: "POST",
            url: "message_ajax.php?CID=" + CID + "&Id="+Id,
            data: dataString,
            cache: false,
            success: function(html){
               $("#flash").fadeOut('slow');
               $("#commentload"+ID).prepend(html);
               $("#update").val('');
               $("#update").focus();
               $("#stexpand").oembed(updateval);
            }
         });
      }
      return false;
   });
   //commment Submit
});

Nenhuma solução correta

Outras dicas

You can get the current URL using location.href (or location.search, since you're only interested in the query portion). Search it for your parameter, for instance using a regex:

if ( /order=1/.test(location.href) )
    $("#commentload"+ID).append(html);
else
    $("#commentload"+ID).prepend(html);

If you need a more general solution, see this related question (or this one, as suggested by @naveen).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top