25

This is a continuation from an existing question. Javascript - Goto URL based on Drop Down Selections (continued!)

I am using dropdown selects to allow my users to build a URL and then hit "Go" to goto it.

Is there any way to add an additional function that checks the URL before going to it?

My URLs sometimes include the "+" character which I need to remove if its the last character in a URL. So it basically needs to be "if last character is +, remove it"

This is my code:

$(window).load(function(){
    $('form').submit(function(e){
        window.location.href = 
            $('#dd0').val() +
            $('#dd1').val()+
            $('#dd2').val()+
            $('#dd3').val();
        e.preventDefault();
    });
});
Community
  • 1
  • 1
Zach Nicodemous
  • 9,097
  • 9
  • 45
  • 68
  • http://stackoverflow.com/questions/280634/endswith-in-javascript/679134#679134 almost exact duplicate – Kevin Jun 06 '11 at 14:33

6 Answers6

42
var url = /* whatever */;

url = url.replace(/\+$/, '');

For example,

> 'foobar+'.replace(/\+$/, '');
  "foobar"
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
36
function removeLastPlus (myUrl)
{
    if (myUrl.substring(myUrl.length-1) == "+")
    {
        myUrl = myUrl.substring(0, myUrl.length-1);
    }

    return myUrl;
}

$(window).load(function(){
    $('form').submit(function(e){
        var newUrl = $('#dd0').val() +
            $('#dd1').val()+
            $('#dd2').val()+
            $('#dd3').val();
        newUrl = removeLastPlus(newUrl);
        window.location.href = newUrl;
        e.preventDefault();
    });
});
Jon
  • 2,502
  • 4
  • 21
  • 23
  • What if the URL ends with more than one plus symbol, say three in a row? – maerics Jun 06 '11 at 14:37
  • @maerics the OP does not seem to be asking about this case. – Matt Ball Jun 06 '11 at 14:41
  • Just playing devil's advocate, from reading OP's code it occurred to me that "dd[0-3]" could contain only plus symbols, but I suppose I'm just being difficult =) – maerics Jun 06 '11 at 14:44
  • I'm not entirely sure how to merge this code with my code. Is it possible? – Zach Nicodemous Jun 06 '11 at 14:52
  • Are you trying to check each of these or just all of them together? `$('#dd0').val() + $('#dd1').val()+ $('#dd2').val()+ $('#dd3').val()` – Jon Jun 06 '11 at 14:57
  • It needs to check the combined result of all 3 together, if that makes sense. – Zach Nicodemous Jun 06 '11 at 15:05
  • It dosen't seem to be working. I will see if I can find the issue. – Zach Nicodemous Jun 06 '11 at 15:22
  • Still no joy. When I hit submit after making selections in my dropdowns, it returns the value of my submit button as the URL. The original code and your code are not talking to one another properly it seems! Would it help if I setup a jsFiddle? – Zach Nicodemous Jun 06 '11 at 15:29
  • This is an example setup on jsFiddle: Original Code: http://jsfiddle.net/AsWBL/ With Modified Code: http://jsfiddle.net/AsWBL/1/ – Zach Nicodemous Jun 06 '11 at 15:46
  • I had ANOTHER typo. I fixed it and now I'm going to try it out on jsFiddle – Jon Jun 06 '11 at 15:52
  • Alright, I fixed my typo and it turns out $('#dd0') didn't exist, so I removed that and it's working now: http://jsfiddle.net/AsWBL/4/ – Jon Jun 06 '11 at 15:54
  • Why use `substr()` and not `charAt()` to retrieve the last character of a string? – Matt Ball May 06 '12 at 15:30
  • He is trying to get everything BUT the last character of the string if it is a "+" character. – Jon May 08 '12 at 23:46
8

Found another solution using str.endsWith("str")

var str = "Hello this is test+";
if(str.endsWith("+")) {
  str = str.slice(0,-1);
  console.log(str)
}
else {
  console.log(str);
}

Also Matt Ball's Replace method looks good. I've updated it to handle the case when there are multiple + at the end.

let str = "hello+++++++++";
str = str.replace(/\++$/, '');
console.log(str);
Black Mamba
  • 13,632
  • 6
  • 82
  • 105
-1

function removeLastPlus(str) {
  if (str.slice(-1) == '+') {
    return str.slice(0, -1);  
  }
  return str;
}
givemesnacks
  • 336
  • 4
  • 12
-1
<script type="text/javascript">
  function truncate_plus(input_string) {
    if(input_string.substr(input_string.length - 1, 1) == '+') {
      return input_string.substr(0, input_string.length - 1);
    }
    else
    {
      return input_string;
    }
  }
</script>
dpmattingly
  • 1,301
  • 1
  • 7
  • 11
-1
$(window).load(function(){
    $('form').submit(function(e){
        var newUrl = $('#dd0').val() +
            $('#dd1').val()+
            $('#dd2').val()+
            $('#dd3').val();
        newUrl = newUrl.replace(/\+$/, '');
        window.location.href = newUrl;
        e.preventDefault();
    });
});

Just seems easier.

Mfoo
  • 3,615
  • 1
  • 16
  • 11