我使用以下代码获取URL,例如domain.com/#2,然后我使用该片段将用户重定向到domain.com/?page=2。
然而,有时用户可能只显示一个散列而没有数字,或者在点击表单时可能在URL中使用关键字,例如/#feedback.
问题是这会导致我使用的代码出现问题。我怎样才能修改提供的代码的方式,将只作用于URL,如果它是我想要的URL。
一种方法是检查片段值是否为"反馈",例如,但我想抓住一个用户可能输入一个值或一个奇怪的形式只是创建一个空白的片段值的方式。
如果URL不包含#(然后是数字)或给定的页面id,则不做任何操作。
所以URL是:
domain.com/#2
将重定向到:
domain.com/?page=2
或者如果URL已经有?page=(number),它会将片段值添加到数字中,因此:
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;
}
首先为页码设置一个全局变量;然后检查"page"查询字符串变量是否设置并且是数字(源)。
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;