rbur0425
rbur0425

Reputation: 479

formatting url in jquery

I am trying to substitute parameters in my url using jquery with my input in my textbox. Also, I need to percent-encode the input. I am trying to use jquery.validator.format however it is not working. Suggestions?

<script src="http://code.jquery.com/jquery-latest.js"></script>
  <script src="http://jqueryjs.googlecode.com/svn/trunk/plugins/validate/jquery.validate.js"></script>
<script language="javascript" type="text/javascript">    
$(document).ready(function () {
        $("button").click(function () {
            var str = "http://api.geonames.org/postalCodeSearchJSON?placename={0}&maxRows=10&username=<my username>";
            alert(str);
            var loc = $("#location").val();
            alert(loc);
            str = jQuery.validator.format(str, loc);
            alert(str);
        });
    });

Upvotes: 1

Views: 4456

Answers (2)

Matt Ball
Matt Ball

Reputation: 359966

This should work just fine, including percent encoding (via encodeURIComponent()):

$("button").click(function () {
    var str = "http://api.geonames.org/postalCodeSearchJSON?placename={0}&maxRows=10&username=redacted";
    var loc = encodeURIComponent($("#location").val());
    str = jQuery.validator.format(str, loc);
    alert(str);
});

jsFiddle demo: http://jsfiddle.net/mattball/FFVCj/

Upvotes: 2

rbur0425
rbur0425

Reputation: 479

 $(document).ready(function () {
    $("button").click(function () {
     //   var str = "http://api.geonames.org/postalCodeSearchJSON?placename={0}&maxRows=10&username=rbur0425";
    //    alert(str);
        var loc = $("#location").val();
  //      alert(loc);
        //    var loc2 = encodeURIComponent(loc);
        var url = "http://api.geonames.org/postalCodeSearchJSON?placename=" + loc + "&maxRows=10&username=rbur0425";
 //       alert(encodeURI(url));
    });
});

works good for me

Upvotes: 0

Related Questions