Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

append /remove anchor name from current url without refresh

I want that on click event to append/remove the anchor name "#on" to be added to the current url without reloading the page, or use the href='#on' from links because it makes my page jump

Eg: http://www.example.com/page.html#on so I can get the detect the users that come from that url and call the On() function

function On() {       //append to current url the anchor "#on"                  }  function Off() {      //remove from the current url the anchor "#on"                     }  $('.on').live('click', function() {   On();   return false;     });    $('.off').live('click', function() {   Off();   return false;     });  
like image 507
Adrian Avatar asked May 08 '11 15:05

Adrian


People also ask

How do I modify the URL without reloading the page?

Method 2: Adding a new state with pushState() Method: The pushState() method is used to add a new history entry with the properties passed as parameters. This will change the current URL to the new state given without reloading the page.

How to remove from URL in anchor tag?

click(function(event){ event. preventDefault(); }); This will stop the links from modifying the urls.

How to remove anchor from URL JavaScript?

Splitting the url (string split) toString(); //Remove anchor from url using the split url = url. split(“#”)[0]; It doesn't get any easier than that. What this code does is grab the URL from the current window, and then splits the string where a '#' is.


2 Answers

You don't really need jQuery for that, you can get/set the anchor name using location.hash. If you put it in your jQuery ready function, you can do some action if it's set to a certain value:

$(function(){     // Remove the # from the hash, as different browsers may or may not include it     var hash = location.hash.replace('#','');      if(hash != ''){         // Show the hash if it's set         alert(hash);          // Clear the hash in the URL         location.hash = '';     } }); 

Note that when removing the hash, the trailing # might stay in the address bar. If you want to respond to live changes of the anchor, you can bind a callback to the hashchange event:

$(document).bind("hashchange", function(){     // Anchor has changed. }); 

If you want to prevent your page jumping to the top when clearing the anchor, you can bind the hashchange event to jump back to the previous scroll position. Check out this example: http://jsfiddle.net/yVf7V/

var lastPos = 0;  $('#on').click(function(){     location.hash = 'blah'; });  $('#off').click(function(){     lastPos = $(window).scrollTop();     location.hash = ''; });  $(window).bind('hashchange',function(event){     var hash = location.hash.replace('#','');     if(hash == '') $(window).scrollTop(lastPos);     alert(hash); }); 
like image 69
DarthJDG Avatar answered Oct 18 '22 09:10

DarthJDG


if you are using jquery try this code

$("a[href^=#]").on("click", function(e) {     e.preventDefault();     history.pushState({}, "", this.href); }); 
like image 20
Alex Doumas Avatar answered Oct 18 '22 10:10

Alex Doumas