0

when i try to do an output for this function, it replaces the space to a + sign. can someone help me please

Picture Containing the issue that i'm facing:

enter image description here

function weightdifference(){
        var weight;
        var dreamweight;
        var calDifference;

        weight = parseFloat(document.bmiform.weight.value);
        dreamweight = parseFloat(document.bmiform.dreamweight.value);


        if (dreamweight > weight){

        calDifference = (dreamweight - weight)/0.064;
        return "You need" +calDifference+ "to your ideal weight";
        }

        else if (dreamweight < weight){

            calDifference = (weight - dreamweight)/0.064;
            return "You need" +calDifference+ "to your ideal weight";
        }

    }

    function results(){
                   var calDifference = weightdifference();
                   var Difference = calDifference;
                   document.bmiform.Difference.value = Difference;
                   document.bmiform.submit();
       }

Results Page

 var difference;
 difference = decodeURIComponent(getUrlVars()["Difference"]);

     Days to Target Weight: <script>document.write(difference)</script><br>

Thanks!

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136

1 Answers1

0

I'm going to guess you're using PHP server-side and using urlencode to encode your parameters. This answer is why I'm guessing that.

If you can change the server side, change it to use rawurlencode instead.

If you can't, and you know + will be encoded (e.g., to %2b) in what you receive, then you can safely do this:

difference = decodeURIComponent(getUrlVars()["Difference"].replace(/\+/g, "%20"));

There, we're replacing + with the standard %20 sequence for a space, and then letting decodeURIComponent do its job.

But if you can, update how the data's being encoded.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875