Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   разбить window.location.href (https://javascript.ru/forum/misc/37728-razbit-window-location-href.html)

Vampir3 04.05.2013 15:18

разбить window.location.href
 
Всем привет, есть ссылка

http://test.ru/index.php?topic=12323&rid=123123

Как получить значение topic?

var topic = window.location.href.replace('http://test.ru/index.php?','').replace(/&rid=[0-9]+/,'')
- не очень красивый способ. Может можно проще?

vadim5june 04.05.2013 15:41

window.location.href.split('=')[1].split('&')[0]

ruslan_mart 04.05.2013 15:53

var topic = location.search.split('&')[0].split('=')[1];

qwerty-клавиатура 04.05.2013 16:58

http://x9a.ru/js/common.js

querystring = {
    parse: function(str, sep, eq) {
        eq = eq || '=';
        var obj = {};

        if (str) {
            var parts = str.split(sep || '&'), 
                i = 0, 
                j = parts.length;

            while (i < j) {
                var temp = parts[i++].split(eq);

                if (temp[0] !== '') {
                    obj[temp[0]] = temp[1] === undefined ? '' : decodeURIComponent(temp[1]);
                }
            }
        }

        return obj;
    },
    
    stringify: function(obj, sep, eq) {
        sep = sep || '&';
        eq = eq || '=';
        var out = [];

        for (var i in obj) {
            out.push( i + (obj[i] !== '' ? eq + encodeURIComponent(obj[i]) : '') );
        }

        return out.join(sep);
    }
};

console.log( querystring.parse( location.search.substr(1) ) );

Vampir3 04.05.2013 21:25

Спасибо, всё ок)

рони 04.05.2013 23:04

Vampir3,
Вариант...
var str = "http://test.ru/index.php?topic=12323&rid=123123";
alert(str.split(/topic=|&/)[1]);

Deff 05.05.2013 02:50

var str = 'http://test.ru/index.php?topic=12323&rid=123123'
alert(parseInt(str.split('topic=')[1]))


Часовой пояс GMT +3, время: 13:09.