2016-09-30 3 views
1

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.Внутренний href <a tag now refreshes the page

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

+0

Можете ли вы показать нам фактическую страницу, демонстрирующую это поведение? –

+1

Я, наконец, решил! Здесь [ссылка] (http://stackoverflow.com/questions/8108836/make-anchor-links-refer-to-the-current-page-when-using-base) и здесь [ссылка] (http: // stackoverflow.com/questions/12303241/how-to-override-base-tag-without-removing-the-tag-itself). Проблема была в теге '. Я должен был использовать JQuery, здесь '$ ("a.local") на ("клик", функция (е) { \t \t e.preventDefault();. \t \t document.location.hash = ""; \t \t document.location.hash = $ (this) .attr ("href"); \t}); ' –

+1

Спасибо @RadLexus, я не знал –

ответ

0

I finally solved it! Check here link и здесь link. Проблема была в теге <base href="/" /> в разделе <head>. Я решил со смесью JQuery и чистого JavaScript (JQuery используется только для отдельных анкеров и получить атрибут), здесь

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

Пожалуйста, обратите внимание, что мне нужно, чтобы установить хэш в пустую строку перед тем как установить его в href анкера с document.location.hash = $(this).attr("href"); не запускает автоматическую прокрутку нескольких кликов на внутреннем якоре (я попробовал)

Смежные вопросы