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();
    });
});
1

6 Answers 6

42
var url = /* whatever */;

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

For example,

> 'foobar+'.replace(/\+$/, '');
  "foobar"
Sign up to request clarification or add additional context in comments.

Comments

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();
    });
});

13 Comments

What if the URL ends with more than one plus symbol, say three in a row?
@maerics the OP does not seem to be asking about this case.
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 =)
I'm not entirely sure how to merge this code with my code. Is it possible?
Are you trying to check each of these or just all of them together? $('#dd0').val() + $('#dd1').val()+ $('#dd2').val()+ $('#dd3').val()
|
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);

Comments

-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>

1 Comment

Why use substr() and not charAt() to retrieve the last character of a string?
-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.

Comments

-1

function removeLastPlus(str) {
  if (str.slice(-1) == '+') {
    return str.slice(0, -1);  
  }
  return str;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.