开发者

Form that removes WWW. and prints result on input?

I need to make a form similar to the one "s开发者_JAVA技巧horten link" sites use. It should simply remove WWW. and echo the result so I later add my code around it.

For example if the user types www.pizza.com/blablabla clicking on input should display: pizza.com/blablabla

Thanks


You can do lots of fancy stuff with regular expressions. For example, this javascript will do what you want:

// Event for enter click
$("#url").keypress(
    function(e) {
        if(e.keyCode == 13) {
            $("#output").html(cleanURL($("#url").val()));
        }
    }
);

// Event for button click
$("#submit").click(
    function() {
        $("#output").html(cleanURL($("#url").val()));
    }
);

// Function to clean url
function cleanURL(url)
{
    if(url.match(/http:\/\//))
    {
        url = url.substring(7);
    }
    if(url.match(/^www\./))
    {
        url = url.substring(4);
    }

    return url;
}

Works on enter click, button click and removes both http:// and www

You can try it out here: http://jsfiddle.net/Codemonkey/ydwAb/1/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜