разбить 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]+/,'')- не очень красивый способ. Может можно проще? |
window.location.href.split('=')[1].split('&')[0] |
var topic = location.search.split('&')[0].split('=')[1]; |
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,
Вариант... var str = "http://test.ru/index.php?topic=12323&rid=123123"; alert(str.split(/topic=|&/)[1]); |
var str = 'http://test.ru/index.php?topic=12323&rid=123123' alert(parseInt(str.split('topic=')[1])) |
Часовой пояс GMT +3, время: 13:09. |