Attaching hashtag to URL with javascript

后端 未结 5 1663
暗喜
暗喜 2020-12-07 20:47

I want to build an ajax site without sacrificing SEO. My question is: If I have link on my page like this:

   

        
相关标签:
5条回答
  • 2020-12-07 21:23

    You cannot set the window.location.href without reloading the page in javascript for security reasons.

    From what I've seen some people are saying Google will index # urls but they will be not considered separate pages and I think that is not what you want. I also have very little experience with SEO.

    0 讨论(0)
  • 2020-12-07 21:32

    You can change the location.hash property, it will change the current anchor identifier without navigating away form the page, for example you could:

    <a href="http://mysite.com/cats" id="cats" class="ajaxLink">Cats</a>
    <a href="http://mysite.com/dogs" id="dogs" class="ajaxLink">Dogs</a>
    

    Then:

    $('.ajaxLink').click(function (e) {
      location.hash = this.id; // get the clicked link id
      e.preventDefault(); // cancel navigation
    
      // get content with Ajax...
    });​
    
    0 讨论(0)
  • 2020-12-07 21:32

    BenMills, noone mentioned location.href, it's about location.hash which does not require page reload.

    0 讨论(0)
  • 2020-12-07 21:39

    Although simplicity is best, but if you just want to automate this process or make it genericise then you can use this lite plugin jquery.hashTag.js

    $('a').hashTag({
        source: function() {
          return $(this).attr('id');
        }
      });
    

    Just put this snippet inside $(document).ready.

    It will do rest of the work itself. Like auto clicking on the link whose id was provided as the hash.

    0 讨论(0)
  • 2020-12-07 21:45

    Google will index a hash if it has an exclamation mark in the form: #!dogs

    It then assumes that these are AJAX-oriented:

    • Google's FAQ about AJAX
    • Google's guide on how to make AJAX website crawlable
    0 讨论(0)
提交回复
热议问题