在“?”之后阅读链接并在javascript中为该链接添加+1

时间:2014-04-22 20:42:41

标签: javascript html

我当前的网页链接是https://www.somesite.com/Vijesti.aspx?vijestID=2122 我怎样才能在""之后读取url参数?并在该页面上建立链接+1或-1? 我需要创建链接,点击进入页面参数?vijestID=2123为+1或?vijestID=2121为-1。谢谢你的帮助

2 个答案:

答案 0 :(得分:0)

如果您要创建“下一个”和“上一个”功能,可以使用location.search获取查询字符串,然后解析它以获取您的值。

if( location.search.indexOf( "vijestID=" ) !== -1 )
    currentId = parseInt(location.search.match(/vijestID=(\d+)/)[1]);

这将只是整数形式的ID,您可以从中添加或减去1来设置您的设置所需的链接。

答案 1 :(得分:0)

您可以将此脚本放置在您想要<a>标记的任何位置,然后根据需要进行修改:

<script>
  (function() {
    var vijestID = parse_query('vijestID');

    var prev = document.createElement('a');
    prev.href = 'https://www.somesite.com/Vijesti.aspx?vijestID=' + (vijestID - 1);
    prev.innerHTML = '<';
    document.body.appendChild(prev);

    var next = document.createElement('a');
    next.href = 'https://www.somesite.com/Vijesti.aspx?vijestID=' + (vijestID + 1);
    next.innerHTML = '>';
    document.body.appendChild(next);

    function parse_query(arg){
        var url = location.href;
        var qs = url.substring(url.indexOf('?') + 1).split('&');
        for(var i = 0, result = {}; i < qs.length; i++){
            qs[i] = qs[i].split('=');
            result[qs[i][0]] = decodeURIComponent(qs[i][1]);
        }
        return arg ? result[arg] : result;
    }
  })();
</script>