开发者

Fixing the URl so there is no spaces

I have a button that has its location generated dynamically via jquery:

<a id="final_location" 
   href="/pre_config/step4" 
   class="proceed-btn right">Pr开发者_JS百科oceed To Next Step &gt;&gt;&gt;</a>
    $('#final_location').click(function() {
        location.href = this.href + '/' + escape($('#type_of_station').html()) + 
            '/' + escape($('.number_changer').attr("id").slice(-1));
        return false;
    });

this works great but the problem comes into play when the html in type_of_station is two words... I get this url:

pre_config/step4/Pizza Delivery/2

Is there a way to make the url only give me the first word like this:

pre_config/step4/Pizza/2

maybe this can be changed to only return the first word?


Use

encodeURIComponent($('#type_of_station').text().match(/^\S*/)[0])

instead of escape($('#type_of_station').html()) to get the first word (separated by space).


This should get you the first word in the phrase:

var type = $('#type_of_station').html();
var i = type.indexOf(' ');
if (i != -1) type = type.substr(0, i);


You could do:

var name = "Pizza Delivery";
var pieces = name.split(" ");

alert(pieces[0]); // Pizza

Or just do:

var name = "Pizza Delivery";
name = name.replace(/\s/g, "-", name); // feel free to change the - delimiter

alert(name); // Pizza-Delivery
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜