如何通过URL将两个变量传递给Javascript?

时间:2016-04-07 18:14:58

标签: javascript jquery pagination

我正在处理我的Tumblr Feed的可搜索存档,我在我的网站上将其用作新闻源。我希望用户能够点击标签来搜索它们,这些标签目前有效。但是当我进入分页时,我迷路了。不可否认,我只是试图将我用其他东西处理的分页代码与我放在一起的标签搜索相结合,所以我相当肯定那里有一个简单的解决方法我只是避难所&# 39; t缠绕我的头。这就是我到目前为止所做的工作。该代码现在将结果限制为两个,因为我还没有很多类似标签的帖子:

$(document).ready(function() {

//create blank Tag array
var tagLinks = [];

//pagination offset
var o = 0;

//tag search from URL variable
var url = window.location.search;
var temp = url.split('=');
var tag = temp[1];

//start on Page 1 of results
if (window.location.search.indexOf('page=') > -1) {
    var url = $(location).attr('href');
    url = url.split("=");
    o = url[1] * 2 - 2;
}

$.ajax({ 
type: 'GET', 
url: 'http://api.tumblr.com/v2/blog/nevermorestudiosonline.tumblr.com/posts?tag=' + tag + '&api_key=<API_KEY_OMITTED>&offset=' + o + '&limit=2', 
data: { get_param: 'value' }, 
dataType: 'jsonp',
success: function (data) { 
            for (i = 0; i < data.response.posts.length; i++) {

                //prep Date
                var dateConv = new Date(data.response.posts[i].date);
                var month = dateConv.getMonth();
                var day = dateConv.getDate();
                var year = dateConv.getFullYear();

                $('#results').append(
                     '<a href="' + data.response.posts[i].short_url + '">'
                            + '<h2>' + data.response.posts[i].title + '</h2>'
                        + '</a>'
                    + '</div>'
                    + '<div class="row">'
                        + '<div class="postedby col-sm-6 col-md-6">'
                            + '<img class="avatar pull-left" src="http://api.tumblr.com/v2/blog/' + data.response.posts[i].post_author + '.tumblr.com/avatar" alt="Avatar" height="64" width="64" />'
                            + '<p>Posted By <br />' + data.response.posts[i].post_author + ' <br />'
                            + '<span class="glyphicon glyphicon-time"></span>&nbsp;'+ (month + 1) +  '/' + day + '/' + year + '</p>'
                        + '</div>'
                        + '<div class="col-sm-6 col-md-6" id="tags_'+i+'">'
                            + '<span class="glyphicon glyphicon-bookmark"></span>'
                        + '</div>'
                    + '</div>'
                +'<hr />');

                //display other tags    
                if (data.response.posts[i].tags.length == 0){
                        $('#tags_'+i).append('&nbsp;No Tags');
                }
                else {
                    for (k=0; k < data.response.posts[i].tags.length; k++){
                        var dashedTag = data.response.posts[i].tags[k].replace(/ /g,"%20");
                        tagLinks.push(dashedTag);
                        $('#tags_'+i).append(
                            '&nbsp;<a href="tagsearch.php?tag=' + tagLinks[k] + '">#' + data.response.posts[i].tags[k] + '</a>'
                        );
                    };
                };

            }

//pagination
var result = 0;
result = data.response.total_posts / 2;

for (j = 1; j <= Math.ceil(result); j++) {
$('#mutlipage').append('<a style="border: 1px solid #ccc; margin: 2px; border-radius: 5px; text-decoration: none; padding: 5px 10px; color: #000;" href="?page=' + j + '">' + j + '</a>');
};


//check log for results to verify accuracy  
console.log(o);
console.log(tag);
console.log(data);
}
});

});

现在,当我尝试简单地将标记变量添加到附加的锚码中时,分页不能正常工作,它会在锚点的末尾添加另一个&amp; page =,直到我最终得到像/ ?tagsearch.php标签=更新和放大器;页=安培;页=安培;页=安培;页= 2。一切都只是......好吧......不行。

0 个答案:

没有答案