0

我最近创建了自己的个人门户页面来取代 iGoogle,因为它将在今年晚些时候关闭。一切正常,除了我拉入的 RSS 提要之一输出如下所示的网址:http ://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNFEguC5pqagsWkkW_y_EjYj9n1bMg&url=http:// www.haaretz.com/news/diplomacy-defense/israel-to-un-replace-austrian-peacekeepers-withdrawn-from-golan-1.528305

单击哪个会转到错误的 url 页面。我将如何删除该网址的前半部分,以便它只有从第二个 http:// 开始的部分

奇怪,但这里的链接工作正常......

刚刚意识到问题是,与号以某种方式变成了破坏链接的实体......

4

3 回答 3

0

尝试这个。一种通用的方法。

function queryString(parameter, url) {
    var a = document.createElement("a");
    a.href = url;        
    var loc = decodeURIComponent(a.search.substring(1, a.search.length));
    var param_value = false;
    var params = loc.split("&");

    for (var i = 0; i < params.length; i++) {
        param_name = params[i].substring(0, params[i].indexOf('='));
        if (param_name == parameter) {
            param_value = params[i].substring(params[i].indexOf('=') + 1)
        }
    }

    if (param_value) {
        return encodeURIComponent(param_value);
    }
    else {
        return "";
        //param not found
    }
}

var secondHTTP = queryString("url", 'http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNFEguC5pqagsWkkW_y_EjYj9n1bMg&url=http://www.haaretz.com/news/diplomacy-defense/israel-to-un-replace-austrian-peacekeepers-withdrawn-from-golan-1.528305');
于 2013-06-06T19:15:45.173 回答
0
var str = "http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNFEguC5pqagsWkkW_y_EjYj9n1bMg&url=http://www.haaretz.com/news/diplomacy-defense/israel-to-un-replace-austrian-peacekeepers-withdrawn-from-golan-1.528305";

var url = decodeURIComponent(str.split(/https?:/ig).pop());

将导致

"//www.haaretz.com/news/diplomacy-defense/israel-to-un-replace-austrian-peacekeepers-withdrawn-from-golan-1.528305"

或者

var url = decodeURIComponent(str.match(/^http.+(http.+)/i)[1]);

将导致

"http://www.haaretz.com/news/diplomacy-defense/israel-to-un-replace-austrian-peacekeepers-withdrawn-from-golan-1.528305"
于 2013-06-06T19:16:20.833 回答
0

编辑:更新代码,添加 jsFiddle


HTML:

<input id="schnitzel" type="text" value="http://www.google.com/http://www.real-foo.bar/" />
<input type="button" onclick="$('#schnitzel').val(window.firstHTTP($('#schnitzel').val()));" value="&raquo;" />

JavaScript:

window.firstHTTP = function (furl = "") {
    var chunked = furl.split("http://");
    return (chunked && chunked[2]) ? ("http://" + chunked[2]) : furl;
};

JS-小提琴:

http://jsfiddle.net/Rm5bU/

于 2013-06-06T19:07:50.390 回答