缩短显示的 URL 并保留开头和结尾(Firebug 'Net' 面板样式) [英] Shorten URL for display with beginning and end preserved (Firebug 'Net' panel style)

查看:24
本文介绍了缩短显示的 URL 并保留开头和结尾(Firebug 'Net' 面板样式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在表格中显示网址,同时将其限制为特定长度.作为一个 URL,最好保留最有意义的部分,这些部分往往是开头和结尾.在 Firebug 'Net' 面板中查看长 URL 时可以看到此功能.

解决方案

这是一个快速而肮脏的解决方案,但到目前为止对我来说效果很好,并且可以轻松更新到任何个人偏好.为了可读性和重用性,它分为两个功能.

该函数使用了如下所示的 shortString 函数.它将 URL 缩短到小于或等于指定的长度 (l),同时保留 URL 的开头和结尾并在首选字符(' '、'/'、'&')处截断.

function shortUrl(url, l){var l = typeof(l) != "undefined" ?升:50;var chunk_l = (l/2);var url = url.replace("http://","").replace("https://","");if(url.length <= l){ 返回 url;}var start_chunk = shortString(url, chunk_l, false);var end_chunk = shortString(url, chunk_l, true);返回 start_chunk + ".." + end_chunk;}

此函数从字符串的开头(或结尾,如果 reverse=true)开始,一旦达到可接受的长度,就开始寻找要截断的首选停止字符.如果在达到指定长度 (l) 之前没有找到停止字符,则返回的字符串将被截断为最大长度.

function shortString(s, l, reverse){var stop_chars = [' ','/', '&'];var可接受的短度= l * 0.80;//什么时候开始寻找停止字符var reverse = typeof(reverse) != "undefined" ?反向:假;var s = 反向 ?s.split("").reverse().join("") : s;var short_s = "";for(var i=0; i < l-1; i++){short_s += s[i];if(i >=acceptable_shortness && stop_chars.indexOf(s[i]) >= 0){休息;}}if(reverse){ return short_s.split("").reverse().join("");}返回short_s;}

示例

<预><代码>>>>var url = "http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/">>>短网址(网址)blog.stackoverflow.com/..swer-your-own-questions/"

I would like to display a URL in a table while restricting it to a specific length. Being a URL, it would be nice to preserve the most meaningful parts which tend to be the beginning and end. This functionality can be seen when viewing long URLs in the Firebug 'Net' panel.

解决方案

This is a quick and dirty solution but it has been working well for me so far and can be easily updated to any individual preferences. It's broken into two functions for readability and reuse.

This function makes use of the shortString function shown below. It shortens a URL to less than or equal to the specified length (l) while preserving the beginning and end of the URL and truncating at preferred characters (' ', '/', '&').

function shortUrl(url, l){
    var l = typeof(l) != "undefined" ? l : 50;
    var chunk_l = (l/2);
    var url = url.replace("http://","").replace("https://","");

    if(url.length <= l){ return url; }

    var start_chunk = shortString(url, chunk_l, false);
    var end_chunk = shortString(url, chunk_l, true);
    return start_chunk + ".." + end_chunk;
}

This function starts at the beginning of a string (or end, if reverse=true) and, once it reaches an acceptable length, starts looking for preferred stop characters to truncate at. If no stop characters are found before the specified length (l) is reached, the string is returned truncated to the max length.

function shortString(s, l, reverse){
    var stop_chars = [' ','/', '&'];
    var acceptable_shortness = l * 0.80; // When to start looking for stop characters
    var reverse = typeof(reverse) != "undefined" ? reverse : false;
    var s = reverse ? s.split("").reverse().join("") : s;
    var short_s = "";

    for(var i=0; i < l-1; i++){
        short_s += s[i];
        if(i >= acceptable_shortness && stop_chars.indexOf(s[i]) >= 0){
            break;
        }
    }
    if(reverse){ return short_s.split("").reverse().join(""); }
    return short_s;
}

Example

>>> var url = "http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/"
>>> shortUrl(url)
"blog.stackoverflow.com/..swer-your-own-questions/"

这篇关于缩短显示的 URL 并保留开头和结尾(Firebug 'Net' 面板样式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆