简体   繁体   English

锚标签在 iPhone Safari 中不起作用

[英]Anchor tag doesn't work in iPhone Safari

I'm having an issue with a named anchor tag in iPhone Safari browser.我在 iPhone Safari 浏览器中遇到了命名锚标记的问题。 It works well in desktop browsers including Safari, but not working in mobile Safari.它适用于包括 Safari 在内的桌面浏览器,但不适用于移动 Safari。 Weird!奇怪的!

For example my URL looks like:例如我的网址看起来像:

http://www.example.com/my-example-article-url-is-like.php#articlebottom

the above URL is from a newsletter and it should go to the bottom paragraph in article page which I gave id like this:上面的 URL 来自时事通讯,它应该转到文章页面的底部段落,我给了这样的 id:

<p id="articlebottom">paragraph is here</p>

When I click the above URL from Newsletter it goes to the article page, but not the bottom para where I specify id.当我从 Newsletter 单击上面的 URL 时,它会转到文章页面,但不会转到我指定 id 的底部段落。 Although I can see that the #articlebottom part is missing from URL when it came into the targeted page in Safari.虽然我可以看到,当它进入 Safari 中的目标页面时,URL 中缺少 #articlebottom 部分。

Any thoughts would be appreciated!任何想法将不胜感激!

Opera, IE, Chrome and Firefox will carry over the anchor to the new page. Opera、IE、Chrome 和 Firefox 会将锚点转移到新页面。 However, Safari loses the anchor on the redirect.但是,Safari 会丢失重定向的锚点。

So what if you add / just before the ID Tag ?那么,如果您在ID Tag之前添加/怎样?

Old URL Path: http://www.example.com/my-example-article-url-is-like.php#articlebottom旧 URL 路径: http://www.example.com/my-example-article-url-is-like.php#articlebottom : http://www.example.com/my-example-article-url-is-like.php#articlebottom

New URL Path: http://www.example.com/my-example-article-url-is-like.php/#articlebottom新 URL 路径: http://www.example.com/my-example-article-url-is-like.php/#articlebottom : http://www.example.com/my-example-article-url-is-like.php/#articlebottom

Another solution is that you'd have to delete both "www."另一种解决方案是您必须删除两个“www”。 from the domain and add a forward slash before the anchor tag in the URL/pathname before Safari would respond properly.并在 Safari 正确响应之前在 URL/路径名中的anchor tag前添加正斜杠。 Firefox doesn't seem to care, and I haven't gotten to testing on IE yet. Firefox 似乎并不关心,我还没有在 IE 上进行测试。

just doing my due diligence and answering for anyone who is having this problem with Index navigation links on a Squarespace page and Safari on iOS.只是做尽职调查并回答任何在 Squarespace 页面上的索引导航链接和 iOS 上的 Safari 上遇到此问题的人。 My anchor links were not working when formatted as such:我的锚链接在格式化时不起作用:

http://sitename.com/page#anchor

Then I tried this to no avail:然后我尝试了这个无济于事:

http://sitename.com/page/#anchor"

Rather than give up, light my computer on fire, and call it a day I tried:与其放弃,不如点燃我的电脑,然后我尝试过:

http://www.sitename.com/page/#anchor

And it worked!它奏效了! So cheers if this helps anyone.如果这对任何人有帮助,那么欢呼吧。

Safari loses the anchor tag after redirecting. Safari 在重定向后丢失锚标记。 So avoid anything that leads to a (second) redirect.所以避免任何导致(第二个)重定向的事情。

  • use the full URL ( http://.. .)使用完整的 URL ( http://... )
  • note if there is a redirect to www or not (www.domain...)请注意是否有重定向到 www (www.domain ...)
  • add a trailing / if there is no prefix like .html/.php (mypage/)添加尾随 / 如果没有像 .html/.php (mypage/) 这样的前缀

    http://www.domain.tdl/mypage/#safari

Maybe it's useful to check for a redirect using tools like cURL也许使用 cURL 之类的工具检查重定向很有用

curl -I http://www.domain.tdl/mypage/#safari

I couldn't get it working with above solutions so hopefully I did my own workaround.我无法使用上述解决方案,所以希望我做了我自己的解决方法。

Purpose: Scroll to anchor "#issues" after navigating to URL "https://example.com/issues/"目的:导航到 URL“https://example.com/issues/”后滚动到锚点“#issues”

  1. Create link "https://example.com/issues?anchor=issues"创建链接“https://example.com/issues?anchor=issues”
  2. On page "https://example.com/issues" add js在页面“https://example.com/issues”上添加js
   <script>
       if (window.location.href.search("anchor=issues") > 0) {
           window.location.href= "https://example.com/issues/#issues";
       }
   </script>

Voila!瞧!

-- ——

You can observe that after opening link "https://example.com/issues/#issues" Safari scrolls from anchor to the top of the page, then when you click to edit URL and hit submit button, you will be scrolled to anchor.您可以观察到打开链接“https://example.com/issues/#issues”后,Safari 从锚点滚动到页面顶部,然后当您单击编辑 URL 并点击提交按钮时,您将滚动到锚点.

I hope they will fix it fast and after that they will add immediately push notification support.我希望他们能快速修复它,然后他们会立即添加推送通知支持。

I have been wrestling with this problem for a client of mine.我一直在为我的一个客户解决这个问题。 I will not name names but I will share a solution I found which suddenly brought dead anchors links back to life and working on both the mobile and desktop versions of the site.我不会说出名字,但我会分享一个我发现的解决方案,它突然让死锚链接恢复生机,并在网站的移动和桌面版本上工作。

I had attributed the malfunction to an overabundance of page / site scripts and css which I can not change because while this site belongs to a local business it is part of a global corporate network which has its own protocols and best practices.我将故障归因于过多的页面/站点脚本和 css,我无法更改,因为虽然该站点属于本地企业,但它是拥有自己的协议和最佳实践的全球企业网络的一部分。 In other words, I can specify inline styles to distinguish page elements but at the end of the day the page must conform to corporate guidelines and rules.换句话说,我可以指定内联样式来区分页面元素,但最终页面必须符合公司指南和规则。

Here's a snippet of my code that embodies what I learned:这是我的代码片段,体现了我所学到的:

href="#anchorname" target="_top"

The key here is the target tag _top这里的关键是目标标签_top

According to http://www.w3schools.com/tags/att_a_target.asp the default target is _self and while your html generator may not write that specific bit of code into your pages which is to say that because it is the default IT DOES NOT HAVE TO BE SPECIFIED, my research has indicated that by specifying _top as the target the dead link problem was solved.根据http://www.w3schools.com/tags/att_a_target.asp ,默认目标是 _self,而您的 html 生成器可能不会将特定的代码写入您的页面,也就是说,因为它是默认的不必指定,我的研究表明,通过将 _top 指定为目标,解决了死链接问题。

I hope this solution works for you.我希望这个解决方案对你有用。

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

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