如何通过javascript从url读取GET元素?

时间:2013-05-17 12:15:17

标签: javascript url get

我的网址如下所示: http://www.something.hu/#/ask?kerdesPost=fdasdas%20ad%20asd%20ad%20asdas

我只想fdasdas%20ad%20asd%20ad%20asdaskerdesPost=fdasdas%20ad%20asd%20ad%20asdas

我如何通过JavaScript完成?什么是最短路?

1 个答案:

答案 0 :(得分:7)

您可以尝试以下操作:

if((window.location.href).indexOf('?') != -1) {
    var queryString = (window.location.href).substr((window.location.href).indexOf('?') + 1); 

    // "queryString" will now contain kerdesPost=fdasdas%20ad%20asd%20ad%20asdas

    var value = (queryString.split('='))[1];

    // "value" will now contain fdasdas%20ad%20asd%20ad%20asdas

    value = decodeURIComponent(value);

    // "value" will now contain fdasdas ad asd ad asdas (unescaped value)
}

JSFiddle

这可以满足您的需求。