检查关键字的网址片段 [英] Checking URL fragment for a keyword
问题描述
我使用以下内容获取网址domain.com/#2,然后我使用该片段将用户重定向到domain.com/?page=2。
但是,有时用户可能只显示一个单一的散列和没有数字,或者我可能会在点击一个表单时在URL中使用关键字,例如/#feedback。
问题是这导致我使用的代码出现问题。我怎样才能修改提供的代码,只会对网址的行为产生影响,如果我想要网址的话。
一种方法是检查片段价值是'反馈',但我想赶上一种方式,用户可能输入一个值或一个奇怪的形式,只是创建一个空白片段值。
如果URL不包含#(然后是数字)或给定的页面ID,那么不做任何事情。
所以URL的地址:
domain.com/#2
将重定向到:
domain.com/?page=2
或者,如果该网址已经有一个?page =(number),它会将片段值添加到该数字中:
domain.com/?page=2#2
将直接发送至:
domain.com/?page=4
我最初认为它检查片段是否为数字,否则将其视为0。
所以这:
/ *
检查URL是否有#值。当用户访问另一个页面/重新加载页面和
时,它包含一个#片段,此代码将转换#值并将用户重定向到页面
,例如domain.php?page = THE FRAGMENT VALUE
如果URL已经包含页面值,它会将该值添加到散列值,
会将用户重定向到新值页面。
* /
//首先获取页面网址并通过#和?拆分它。标志
var parts = location.href.split('#');
var queryParameters = location.search.split('?');
//现在,如果有一个
var pageNumber = 0,那么我们可以在URL中获取页面值的值。
for(var i = 0; i< queryParameters.length; i ++)
{
var keyvaluePair = queryParameters [i] .split('=');
if(keyvaluePair [0] =='page')
{
pageNumber = keyvaluePair [1];
休息;
}
}
//接下来我们检查URL中有多少部分,如果这是一个值,我们将它添加到当前页面
//并重定向到新的页码
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;
首先设置一个全局变量页码;然后检查页面查询字符串变量是否设置为数字(源)。
var pageNum = 0;
$ b $ function getParameterByName(name){
name = name.replace(/ [\ [] /,\\ [)。replace(/ [\]] / ,\\);
var regex = new RegExp([\\?&]+ name +=([^&#] *)),
results = regex.exec(location。搜索);
返回结果== null? :decodeURIComponent(results [1] .replace(/ \ + / g,)); $!
$ b 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){
casefeedback:
window.location.href = 'domain.com/feedback';
休息;
默认值:
//什么都不做?
休息;
$ b $ p
$ b 最后,如果 pageNum> 0
。
if(pageNum> 0)window.location.href ='domain.com /?page ='+ pageNum;
完整的代码:
var pageNum = 0;
$ b $ function getParameterByName(name){
name = name.replace(/ [\ [] /,\\ [)。replace(/ [\]] / ,\\);
var regex = new RegExp([\\?&]+ name +=([^&#] *)),
results = regex.exec(location。搜索);
返回结果== null? :decodeURIComponent(results [1] .replace(/ \ + / g,)); $!
$ b 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){
casefeedback:
window.location.href = 'domain.com/feedback';
休息;
默认值:
//什么都不做?
休息;
}
}
if(pageNum> 0)window.location.href ='domain.com/?page='+ pageNum;
i use the following to take a URL e.g. domain.com/#2 and then i use that fragment to redirect the users to domain.com/?page=2.
However, sometimes the user may be shown just a single hash and no number or i might use a keyword in the URL when clicking a form e.g. /#feedback.
The problem is this causes an issue with the code i use. How can i modify the provided code in a way that will only act upon the URL if its how i want the URL to be.
One way is to check if the fragment value is 'feedback' for example, but i would like to catch a way for a user perhaps entering a value or an odd form just creating a blank fragment value.
If the URL doesnt contain a #(then a number) or a given page id then dont do anything.
So the URL of:
domain.com/#2
Will redirect to:
domain.com/?page=2
Or if the URL already has a ?page=(number) it will add the fragment value to the number so:
domain.com/?page=2#2
Will direct to:
domain.com/?page=4
My initial thought it checking if the fragment is numeric, otherwise treat it as a 0.
So this:
/*
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;
}
解决方案 First set a global variable for the page number; then check if the "page" query string variable is set and is numeric (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'));
Then check window.location.hash
for the hash. If it is numeric, add it to pageNum
. Else check if it is a command.
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;
}
}
Finally, redirect the user if pageNum > 0
.
if(pageNum > 0) window.location.href = 'domain.com/?page=' + pageNum;
Complete code:
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;
这篇关于检查关键字的网址片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!