检查关键字的URL片段

时间:2014-06-23 12:56:43

标签: javascript php jquery url fragment

我使用以下内容来获取网址,例如domain.com/#2然后我使用该片段将用户重定向到domain.com/?page=2。

但是,有时用户可能只会显示一个哈希而没有数字,或者我可能会在点击表单时使用网址中的关键字,例如/#反馈。

问题是这会导致我使用的代码出现问题。我如何修改提供的代码,只有在我想要URL的情况下才会对URL起作用。

一种方法是检查片段值是否为'反馈'例如,但我想为用户提供一种方法,可能只需创建空白片段值即可输入值或奇数形式。

如果网址不包含#(然后是数字)或给定的网页ID,则不执行任何操作。

所以URL:

domain.com/#2

将重定向到:

domain.com/?page=2

或者如果URL已经有?page =(数字),它会将片段值添加到数字中,以便:

domain.com/?page=2#2

将指示:

domain.com/?page=4

我最初认为它检查片段是否为数字,否则将其视为0。

所以这个:

/* 
Check if the URL has a # value. When a user visits a page from another / reloads the page and
it has a # fragment included, this code converts the # value and redirects the user to a page
such as domain.php?page=THE FRAGMENT VALUE
If the URL already contains a page value, it adds that value to the hash value and will
redirect the user to the new value page.
*/

// First get the page URL and split it via # and ? signs
var parts = location.href.split('#');
var queryParameters = location.search.split('?');

// Now we get the value of the page value in the URL if there is one
var pageNumber = 0;
for(var i = 0; i < queryParameters.length; i++)
{
  var keyvaluePair = queryParameters[i].split('=');
  if(keyvaluePair[0] == 'page')
  {
    pageNumber = keyvaluePair[1];
    break;
  }
}

// Next we check how many parts there are in the URL and if this a value, we add it to the current page
// and redirect to that new page number
if(parts.length > 1)
{
  var params = parts[0].split('?');
  var mark = '?';
  if(params.length > 1)
{
mark = '?';
}
var newPageNumber = parseInt(parts[1], 10) + parseInt(pageNumber, 10);
location.href = mark + 'page=' + newPageNumber;
}

1 个答案:

答案 0 :(得分:0)

首先为页码设置一个全局变量;然后检查“page”查询字符串变量是否已设置且是否为数字(source)。

var pageNum = 0;

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

if(!isNaN(getParameterByName('page'))) pageNum += parseInt(getParameterByName('page'));

然后检查window.location.hash是否有哈希值。如果是数字,请将其添加到pageNum。否则检查它是否是一个命令。

    if(!isNaN(parseInt(window.location.hash.replace(/#/, '')))) {
    pageNum += parseInt(window.location.hash.replace(/#/, ''));
} else if(window.location.hash != null) {
    switch(window.location.hash) {
        case "feedback":
            window.location.href = 'domain.com/feedback';
            break;
        default: 
            // do nothing?
            break;
    }
}

最后,如果pageNum > 0,则重定向用户。

if(pageNum > 0) window.location.href = 'domain.com/?page=' + pageNum;

完整代码:

var pageNum = 0;

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

if(!isNaN(getParameterByName('page'))) pageNum += parseInt(getParameterByName('page'));

if(!isNaN(parseInt(window.location.hash.replace(/#/, '')))) {
    pageNum += parseInt(window.location.hash.replace(/#/, ''));
} else if(window.location.hash != null) {
    switch(window.location.hash) {
        case "feedback":
            window.location.href = 'domain.com/feedback';
            break;
        default: 
            // do nothing?
            break;
    }
}

if(pageNum > 0) window.location.href = 'domain.com/?page=' + pageNum;