javascript - 检查关键字的 URL 片段

标签 javascript php jquery url fragment

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

但是,有时用户可能只看到一个散列而没有数字,或者我可能在单击表单时在 URL 中使用关键字,例如/#反馈。

问题是这会导致我使用的代码出现问题。如果 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”查询字符串变量是否已设置并且是数字(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;

关于javascript - 检查关键字的 URL 片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24366549/

相关文章:

jquery - 在 iOS 设备上关闭 CSS 菜单

javascript - 将 youtube 视频 url 转换为嵌入代码

javascript - 如何从数组中获取javascript中的列表

javascript - 滚动时填充 SVG 描边并删除向上滚动时的填充

javascript - Safari 扩展中的重定向 url

javascript - 传单和 MongoDB 半径

SimpleXMLELement 的 PHP json_encode 在不同的环境下给出不同的结果

php - 当我尝试使用 Laravel Controller 方法处理 AJAX 请求时,为什么会出现此异常?

PHP:使用变量值作为变量名:对语法问题感到好奇

jquery - 在单独的表中呈现 Mysql 数据取决于值