<a tag="" now="" refreshes="" the="" page=""

时间:2016-09-30 17:55:22

标签: .htaccess redirect anchor internal

="" I've got several anchor tags pointing to internal links mainly to scroll to some section titles or to get to the top from a bottom link of the site since some pages can get very long.

All tags like <a href="#whatever">Whatever</a> just scrolled the page to the <a name="whatever"></a> tag as intended until yesterday, but now when clicked they force a page refresh pointing to the root page with just hash, like this localhost/#whatever instead of this localhost/path/to/current/page/#whatever.

1) Is there even the possibility to alter something in the Apache server, browser settings (not touched tough), HTML/JavaScript/CSS code of a page or whatever to force page refreshes when clicking on internal links?

2) If I change the anchor to <a href="path/to/current/page/#whatever> it works, but it's just because a page refresh triggers and then the page is scrolled like normal when interpreting the hash fragment. Also, this way I loose any GET parameters (I can't predict them) which I really need since it's a database website

3) If I alter or remove the <base href="/" /> tag nothing happens, still the internal links worked before with that tag in place

4) I recently updated the .htaccess file and that could potentially be the cause but still routing has no problems and I can't see why any RewriteRule could possibly affect internal links. Also, trying to revert it to previous version didn't help

5) Same behavior applies to both Firefox and Chrome, latest versions

6) I tried to create a test page in the same environment (same .htaccess, same HTML base template) with just a very long <ul> list containing list elements with integers in sequence until 500, then a <a href="#20">To 20</a> at the bottom of the page and it just worked all good... What can force a internal link to redirect?! Please help

1 个答案:

答案 0 :(得分:0)

我终于解决了!点击此处linklink。问题出在<base href="/" />部分的<head>标记中。我用jQuery和纯JavaScript的混合解决了(jQuery仅用于选择锚点和get属性),这里

$("a.local").on("click", function (e) {
    e.preventDefault();
    document.location.hash = "";
    document.location.hash = $(this).attr("href");
});

请注意,我需要将哈希设置为空字符串,然后才将其实际设置为锚点的href,因为单独document.location.hash = $(this).attr("href");不会触发内部锚点上的多次点击时自动滚动(我试过)