Jquery 当URL已添加?source=时,哈希标记无法显示Div

Jquery 当URL已添加?source=时,哈希标记无法显示Div,jquery,fancybox-2,hashtag,window.location,deep-linking,Jquery,Fancybox 2,Hashtag,Window.location,Deep Linking,我正在用传入的HashTags启动FancyBox2。一切都很好。。。直到您将源代码添加到URL。我怎样才能让它工作?下面是代码。我假设我需要说hash=直到a?如果是?存在 Jquery: var thisHash = window.location.hash; if(window.location.hash) { $(thisHash).fancybox().trigger('click'); } 需要工作的链接: www.mysite.com/test/test-这很有效 www.mys

我正在用传入的HashTags启动FancyBox2。一切都很好。。。直到您将源代码添加到URL。我怎样才能让它工作?下面是代码。我假设我需要说hash=直到a?如果是?存在

Jquery:

var thisHash = window.location.hash;
if(window.location.hash) {
$(thisHash).fancybox().trigger('click');
}
需要工作的链接:

www.mysite.com/test/test-这很有效

www.mysite.com/test/test?source=blahblahblah-这不是

www.mysite.com/test/?source=blahblahblahtest-这很有效lol

快速回答:哈希标记/命名锚/片段标识符(无论您如何调用它们)必须位于查询字符串之后,以便至少按照您的预期正确解析。因此,您需要相应地构建URL

superuser.com上的以下问题对您的问题有完整的答案,尽管您的问题有点不同,但答案是相同的:

虽然http://www.example.com/test/test?source=blahblahblah 是有效的URL,将被视为哈希标记字符串的是整个测试?source=blahblahblah部分,未检测到任何查询字符串。此外,如果需要在URL的路径、文档或查询字符串部分包含字符,则必须将其赋为%23

所以不,你的第二个链接永远不会像你所希望的那样工作,即使你需要它这样工作

编辑奖金

如果您正在寻找在PHP中向现有url添加查询字符串的简单方法,请尝试以下代码中的url_add_query_参数:

 function build_query($query_data, $numeric_prefix = '', $arg_separator = '&', $inner_call = 0) {
    // Source: http://php.net/manual/en/function.http-build-query.php#112024
    if (!is_array($query_data)) return false;
    $result = array();
    foreach ((array)$query_data as $key => $value) {
        if ($inner_call) {
            if (is_numeric($key))
                $key = $numeric_prefix . "[]";
            else
                $key = $numeric_prefix . "[$key]";
        } else {
           if (is_int($key))
                $key = $numeric_prefix . $key;
        }

        if (is_array($value) || is_object($value)) {
            $result[] = build_query($value, $key, $arg_separator, 1);
                continue;
        }
        $result[] = urlencode($key) . "=" . urlencode($value);
    }
    return implode($arg_separator, $result);
}

function unparse_url($parsed_url) {
      // Source: http://php.net/manual/en/function.parse-url.php#106731
      $scheme   = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
      $host     = isset($parsed_url['host']) ? $parsed_url['host'] : '';
      $port     = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
      $user     = isset($parsed_url['user']) ? $parsed_url['user'] : '';
      $pass     = isset($parsed_url['pass']) ? ':' . $parsed_url['pass']  : '';
      $pass     = ($user || $pass) ? "$pass@" : '';
      $path     = isset($parsed_url['path']) ? $parsed_url['path'] : '';
      $query    = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
      $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
      return "$scheme$user$pass$host$port$path$query$fragment";
}

function url_add_query_params($url, $new_params) {
    // parse url
    $url_components = parse_url($url);
    // return immediately if parsing failed
    if ($url_components === FALSE) { return $url; }
    // if $new_params is a query string, parse it
    if (!is_array($new_params)) {
        $new_params_str = $new_params;
        $new_params = array();
        parse_str($new_params_str, $new_params);
    }
    // parse query string from original url
    $params_str = isset($url_components['query']) ? $url_components['query'] : '';
    $params = array();
    parse_str($params_str, $params);
    // merge the two arrays, keeping later values for identical keys
    $params = array_merge($params, $new_params);
    // rebuild query string
    $url_components['query'] = build_query($params);
    // rebuild url
    return unparse_url($url_components);
}
例如:

$url = 'http://www.example.com/test/#test';
$new_url = url_add_query_params($url, 'source=blahblahblah');
echo $new_url; // output: http://www.example.com/test/?source=blahblahblah#test
echo "\n";

$url = 'http://www.example.com/test/?source=index&param=value#test';
$new_url = url_add_query_params($url, 'source=blahblahblah');
echo $new_url; // output: http://www.example.com/test/?source=blahblahblah&param=value#test
我只是对它进行了轻微的测试,但它应该可以正常工作。

这个www.mysite.com/test/test?source=blahblahblah链接不起作用的原因是变量:

var thisHash = window.location.hash
。。。将返回测试?源=blahblahblah

那么您正试图将此哈希测试?source=blahblahblah绑定到fancybox

$(thisHash).fancybox().trigger('click');
。。。这是一个无效的选择器

您可能需要编写一个函数来验证window.location.hash并提取要绑定到fancybox的正确选择器,如:

var _param;
function getSelector(thisHash) {
    if (thisHash.indexOf("?")) {
        _param = thisHash.split("?");
        return _param[0];
    } else {
        return thisHash;
    };
};
检查以下JSIDLE链接:

然后,您可以将fancybox绑定到适当的选择器,并动态添加尾部参数?source=:

var thisHash = window.location.hash;
jQuery(document).ready(function ($) {
    if (window.location.hash) {
        var selector = getSelector(thisHash);
        $(selector).fancybox({
            beforeLoad: function () {
                this.href = this.href + (typeof _param == "undefined" ? "" : "?" + _param[1])
            }
        }).trigger('click');
    }
}); // ready

谢谢你的帖子-我明白你的意思,我现在将遵循你的说法,但我可以想象你可以编写代码来查找散列,并在达到?标记后停止。然后取下那根绳子,从盒子里开枪。我想您需要将正则表达式与jQuery一起使用。因此,对散列的处理与对散列的处理有点不同。问题是,即使您修补/修复jQuery,浏览器和HTTP服务器的行为也将是相同的,即,从请求中剥离后的任何查询参数,而将其作为片段标识符的一部分。我确信您不想编写自己的浏览器或服务器。如果URL是www.mysite.com/?sourecodeHashTag怎么办。上面的代码会不会导致这不起作用呢?我回答了“不起作用”链接,但我打赌你基本上知道发生了什么,以及如何解决它,不是吗?除非你想让我写完整的食谱