Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove '#' from a URL in JQuery

I have written a script remove the # from URL & scroll to the particular div. Its working for the normal url like

10.0.1.22/dev/pg/blog/all/

But its not working for the URL like

10.0.1.22/dev/pg/search/?tag=a&entity_subtype=blog&entity_type=object&search_type=entities#39531

Its not removing the # from URL & also the scrolling also not working.

Script

    jQuery(document).ready(function($) {

    $(".full_view_btn").live('click', function(){
        var divid = $(this).attr('data-id');
        var long_desc = "#inner_descrip" + divid;   
        var short_desc = "#listing_desp" + divid;
        var shw_mre_btn = "#full_view" + divid;

        if($(long_desc).is( ':hidden' )){
            $('.long_desc').hide();
            $('.short_desc').show();
            $(long_desc).show();
            $(short_desc).hide();
            $('.full_view_btn').text('Show more');
            $(shw_mre_btn).text('Show less');
          } else{
              $(long_desc).hide();
              $(short_desc).show();
              $(shw_mre_btn).text('Show more');
          }

        if (location.pathname.replace('/^\//','') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
          var target = $(this.hash);
          target = target.length ? target : $('[id=' + this.hash.slice(1) +']');
          if (target.length) {
            $('html,body').animate({
              scrollTop: target.offset().top-60
            }, 500);
                target.parent().find('#highlight_div'+divid).css("box-shadow", "0 0 2px 1px #cccccc");
            setTimeout(function() {
                target.parent().find('#highlight_div'+divid).css("box-shadow", "none");
            }, 1000);
            return false;
          }
        }

    });

});

Any idea guys how to fix this ? I cant find where is the error ?

like image 417
UI Dev Avatar asked Dec 06 '25 16:12

UI Dev


1 Answers

you cannot use jQuery object to wrap hash string otherwise you will get empty array because the input parameter for jQuery should be an selector. You need target as DOM element wrapped by jQuery object to use methods like offset.

So you have to change the target to or whatever should be the target to scroll down

var target = $(this.target);
like image 132
Martin Surynek Avatar answered Dec 08 '25 04:12

Martin Surynek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!