UPDATES VIA   RSS  |  Email Get updates via feedburner Get updates via twitter
Home / Apps / Coding / JavaScript & Ajax / PHP / JavaScript: Get anchor from …

JavaScript: Get anchor from URL

Posted on 10. Jun, 2009 by in Apps, Coding, JavaScript & Ajax, PHP

To get the anchor from an URL (this is the text after the # character) in JavaScript you can use the following code:

var url='http://iwebdevel.com/some-post#comment-1'; //if you need to take the current url of the window use: var url=window.location;
var anchor=url.hash; //anchor with the # character
var anchor2=url.hash.substring(1); //anchor without the # character

If you’re iterating through the links in a page with JQuery, and $(this) defines a single link, then to find the anchor of this object you’d need to use the following code:

var anchor=$(this).attr("hash");
Translate this post
                    
                    

Related posts:

  1. Javascript: How to validate email address with JavaScript?
  2. Best websites (top stories) on popular social bookmarking services Facebook application/iGoogle gadget
  3. JavaScript: How to get the index (position within a group) of an object with jQuery?
  4. JavaScript: Send function as a parameter to another function (callbacks)
  5. JavaScript: What if jQuery animation doesn’t fire/start?

  • Kago Kagichiri

    Thank you! You were a click away, straight to the point and you have a jQuery alternative. Awesome.

  • Aylingumus

    Tüm
    dünyadaki okey oyuncuları ile aynı platform içerisinde sohbet ederek canlı okey
    oynamaya hazır olun. http://www.okey-oyna.com sizi eğlenceye ve heyecana davet ediyor. İnternette görüntülü
    olarak okey oyna, gerçek kişilerle tanış, turnuva
    heyecanını yaşa. Dünyanın en büyük internet oyun portalı içerisine kayıt olun
    ve oynamaya hemen başlayın.