0

I'm looking for a way of removing the url.com/index.html#content. So I'm looking for a way to remove the #content part (posibly with jQuery?). Is there any way to achieve this after I've already clicked on the link?

PowerUser
  • 812
  • 1
  • 11
  • 32
  • Do you want to consume the URL in your code or do you want the browser address bar URL changed? – Gautam Bhutani Mar 18 '14 at 18:45
  • http://stackoverflow.com/questions/1397329/how-to-remove-the-hash-from-window-location-with-javascript-without-page-refresh – Stuart Kershaw Mar 18 '14 at 18:49
  • Yeah but the hash redirects to an element with the `id="content"` so first I don't know where to put this script (`` section maybe?). Anyway more detailed example than just a link to a random question won't help me. Could I have an example of the whole code? If yes I'll mark it as answer. Thanks in advance. – PowerUser Mar 18 '14 at 19:11

1 Answers1

0

Anyway here's the solution I've found:

HTML Markup:

<a href='#content' onclick="return anchorJump('content')">Skip to Content</a>

And here's the JavaScript:

    function anchorJump( anchor )
{
  var targAnchor=null, anchorID=anchor.replace(/.*#([^\?]+).*/,'$1');   

  if( !(targAnchor=document.getElementById( anchorID )) )
   for(var i=0, found=false, da=document.anchors, len=da.length; i<len && !targAnchor; i++)
    targAnchor = (da[i].name==anchorID ? da[i] : null); 

  if(targAnchor)  
  {
   disp=getElemOffset(targAnchor);
   window.scrollTo(disp.x, disp.y);
  }
  else
   alert('Did not find anchor/element "'+anchorID+'"');

  function getElemOffset(elem)
  {
   var left = !!elem.offsetLeft ? elem.offsetLeft : 0;
   var top = !!elem.offsetTop ? elem.offsetTop : 0;

   while((elem = elem.offsetParent))
   { 
    left += !!elem.offsetLeft ? elem.offsetLeft : 0;
    top += !!elem.offsetTop ? elem.offsetTop : 0;
   }

   return {x:left, y:top};  
  }

 return false; 
}
PowerUser
  • 812
  • 1
  • 11
  • 32