简体   繁体   English

在HTML中添加锚标记和潜在的JavaScript干扰问题

[英]Adding anchor tag in html and potential javascript interfering issue

I have two html pages, in which on first page I have a link that points to an anchor tag to the second page. 我有两个html页面,其中在第一页上有一个指向第二页的anchor tag的链接。

Here is the first page. 是第一页。 At the bottom of the page there are some links. 页面底部有一些链接。 If user clicks on for example Analog Electronics link s/he will go to the second page that has the anchor tag . 例如,如果用户单击“ Analog Electronics链接,则他/她将进入带有anchor tag的第二页。

Here is the link on the first page: 这是首页上的链接:

<a href="/electronics.html#electronics-books-suggested-by-hooman-darabi">Electronics</a>

and here is the anchor tag on the second page: 这是第二页上的anchor tag

<h3 id="electronics-books-suggested-by-hooman-darabi">Electronics books suggested by Hooman Darabi</h3>

At first the anchoring was not working on Chrome, but working on Firefox. 最初,锚定不适用于Chrome,但适用于Firefox。 After some Google search I came up with some clues: 经过一番Google搜索,我想出了一些线索:

In the bottom of my html file I have three JavaScript files included: 在我的html文件的底部,我包含了三个JavaScript文件:

If I comment out any of these 3, the anchoring works fine on both Chrome and FireFox. 如果我注释掉这3个中的任何一个,则anchoring在Chrome和FireFox上都可以正常工作。 But of course I need these 3 JS files to be included on my page. 但是,当然,我需要将这3个JS文件包含在我的页面中。

After more research I found this suggestion: 经过更多研究,我发现了这一建议:

To get the anchoring to work and when the link is clicked the top of the presented second page to be at the anchor tag position I had to add this code to the html page: 为了使anchoring正常工作,当单击链接时,出现的第二页顶部位于anchor tag位置,我必须将此代码添加到html页:

$(document).ready(function () {
            var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
            if (window.location.hash && isChrome) {
                setTimeout(function () {
                    var hash = window.location.hash;
                    window.location.hash = "";
                    window.location.hash = hash;
                }, 200);
            }
        });

I don't know what this code does, but it helps the anchored to be shown at the top of the presented linked page. 我不知道这段代码的作用,但是它可以帮助锚定在显示的链接页面顶部显示。

Although this code solves my issue, but now something bad happens!. 尽管这段代码解决了我的问题,但是现在发生了一些不好的事情!。 If on the second page ( http://www.doradolist.com/analog.html#analog-electronics-books-suggested-by-hooman-darabi ) I click on the Chrome back bottom I don't go back to previous page. 如果在第二页( http://www.doradolist.com/analog.html#analog-electronics-books-suggested-by-hooman-darabi )上,我单击了Chrome后底部,但我不返回上一页。 I need to press the back bottom 3 times to go back to previous page. 我需要按后下3次才能返回上一页。 If I look at what happens to the URL after each back bottom press I see this in the URL of the page: 如果我查看每次按下后底部后URL发生了什么,我会在页面的URL中看到以下内容:

Original URL: 原始网址:

http://www.doradolist.com/analog.html#analog-electronics-books-suggested-by-hooman-darabi

After one back bottom click: 在一个后底下单击:

http://www.doradolist.com/analog.html#

After two back bottom clicks: 经过两次后底点击:

http://www.doradolist.com/analog.html#analog-electronics-books-suggested-by-hooman-darabi

and finally after 3 back bottom clicks I go to the original page. 最后,在单击了3个后底按钮之后,我转到原始页面。

Any help on why this back bottom issue happens and how to resolve it would be greatly appreciated. 我们将非常感谢您提供任何有关为何发生此后勤问题以及如何解决此问题的帮助。 Or if instead of adding the code that I have shown above there is another way to resolve the anchoring issue in Chrome that would be even better because the back bottom issue is the result of that code. 或者,如果不添加我上面显示的代码,而是采用另一种方法解决Chrome中的anchoring问题,那就更好了,因为底层问题是该代码的结果。

To answer your question of why this is happening, I'll explain what that snippet of code is doing. 要回答你为什么会这样的问题,我将解释的代码片段所做的工作。

Once you land on the second page (the first URL in history), this snippet waits for the document to be loaded ( $(document).ready ), then executes the callback passed to it, which checks if the browser is Chrome, if it is then it adds a callback onto the message bus ( setTimeout ), which grabs the hash from the URL, it removes the hash from the URL (the second URL in history), then adds it back to the URL (the third URL in history, and the one which ultimately 'scrolls' to that ID). 进入第二页(历史记录中的第一个URL)后,此代码段将等待文档加载( $(document).ready ),然后执行传递给它的回调,该回调检查浏览器是否为Chrome,是否然后在消息总线( setTimeout )上添加回调,该回调从URL中获取哈希,从URL(历史记录中的第二个URL)中删除哈希,然后将其添加回URL中(在URL中,第三个URL)。历史记录,以及最终“滚动”至该ID的记录)。

I've annotated your code with comments. 我已经用注释注释了您的代码。

$(document).ready(function () { // wait for document to load
            var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor); // check if browser is Chrome
            if (window.location.hash && isChrome) { // if the URL has a hash in it, and the browser is Chrome
                setTimeout(function () { // add this callback to the message bus
                    var hash = window.location.hash; // read the hash from the URL in the address bar
                    window.location.hash = ""; // set the hash in the URL in the address bar to be empty
                    window.location.hash = hash; // add the hash back to the URL in the address bar
                }, 200);
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM