简体   繁体   English

为什么其他站点/服务看不到我的哈希URL?

[英]Why don't other sites/services see my hash URLs?

My site has all dynamically loaded content. 我的网站具有所有动态加载的内容。

I have written a few JS functions that change the content based on the URL received. 我写了一些JS函数,它们根据收到的URL更改内容。 If someone goes to www.mysite.com/#1056, the content for that will be loaded. 如果有人访问www.mysite.com/#1056,将会加载该内容。

function getLocationHash() {
    //check if there is a location hash in the address bar, get that URL
    if (window.location.hash != '') {
        processURL()
    }
}

Then it calls the processURL function 然后调用processURL函数

function processURL() {
    if (window.location.hash != '') {
        urlHash = window.location.hash;

        //if it's a catalog item, it has a number above #1000
        if (urlHash > 10000) {
            getDetail(urlHash);
        }

This works fine for history or jumping right to a URL on the site - however, other sites cannot follow this. 这适用于历史记录或直接跳至该站点上的URL-但是,其他站点无法遵循此规则。 For instance, if I enter www.mysite.com/#1056 into Facebook status, FB scrapes only www.mysite.com index page. 例如,如果我在Facebook状态中输入www.mysite.com/#1056,则FB仅抓取www.mysite.com索引页。 It does not follow through to the end of the JS. 它不会一直持续到JS的末尾。 Is this because the JS is looking for the 'window' property? 这是因为JS正在寻找“窗口”属性吗?

Same thing with Google crawling. Google抓取也是一样。 I set up a sitemap with all of the hashed URLs but Google only crawls the index page. 我使用所有哈希网址设置了站点地图,但Google只抓取索引页面。

So the question is: How do I take what I have here and properly format a URL that other services like Facebook and Google can "see"? 因此,问题是:我该如何利用这里的内容,并正确设置其他服务(如Facebook和Google)可以“看到”的URL?

Any tips would be much appreciated. 任何提示将不胜感激。

The # indicates the start of the fragment identifier. #表示片段标识符的开始。 It is how you link to part of a page. 这是您链接到页面一部分的方式。

It is frequently abused to be read by JavaScript to load different content via Ajax, but that only works if the client runs the JS. 它经常被JavaScript读取以滥用其通过Ajax加载不同的内容,但这仅在客户端运行JS时有效。

The scrapers used by Google and Facebook don't run JS. Google和Facebook使用的抓取工具没有运行JS。

  1. Stop using fragment identifiers to load content 停止使用片段标识符加载内容
  2. Use real URLs instead 改用真实网址
  3. Have the server deliver complete pages for those URLs 让服务器为这些URL提供完整的页面
  4. Apply your Ajax changes using the history API to update the URI to match the one that would load the page you are creating with JS directly 使用历史记录API应用Ajax更改以更新URI,以匹配将直接加载使用JS创建的页面的URI。

These are the solutions i discovered when i researched this. 这些是我在研究时发现的解决方案。

For crawling there is the 'hashbang' , as described in the google pages. 如谷歌页面所述,有用于爬网的“ hashbang”。 https://developers.google.com/webmasters/ajax-crawling/docs/learn-more?hl=nl https://developers.google.com/webmasters/ajax-crawling/docs/learn-more?hl=nl

And for the linking on facebook you can for example use html5 pushstate. 对于facebook上的链接,您可以例如使用html5 pushstate。 http://badassjs.com/post/840846392/location-hash-is-dead-long-live-html5-pushstate http://badassjs.com/post/840846392/location-hash-is-dead-long-live-html5-pushstate

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

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