检查URL片段中的关键字
Checking URL fragment for a keyword
我使用以下代码获取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;
相关文章:
- 使用“;这个“;JavaScript原型方法中的关键字
- delete关键字在全局变量上的不同行为
- JavaScript 中的嵌套函数和 “this” 关键字
- 如何正确地使这个js片段内容不可知
- 如何在不使用 new 关键字的情况下从函数创建对象
- Angularjs使用“;这个“;promise内的关键字回调
- 重写需要javascript中带有import关键字的语法
- Regex,用于从字符串中筛选关键字
- Javascript 对象和 this 关键字
- 如何在不知道关键字的情况下访问javascript对象值
- 可以在Javascript中添加新的关键字吗
- 具有服务器端呈现的HTML片段的主干模型
- 我的html表单无法验证.请参阅代码片段中的html代码和java脚本
- javascript中的这个关键字
- Javascript关键字搜索子类Div值
- 在对象中用Javascript编写此关键字
- 在构建字典时,作为关键字的变量会自动变为字符串
- 正则表达式,从一组关键字中获取奇数
- 当在文本框中搜索关键字时,我会得到以前的结果作为输出
- 检查URL片段中的关键字