M
M
Mikhail Gilmutdinov2015-08-20 13:14:57
JavaScript
Mikhail Gilmutdinov, 2015-08-20 13:14:57

Why does JavaScript work in jsfiddler but not on the site?

jsfiddle.net/mixailhr/p4vdanuk

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Zuev, 2015-08-20
@Mixailhr

You have the following code on your site

$('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//, '') === this.pathname.replace(/^\//, '') && location.hostname === this.hostname) {

      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });

Do you know what return false does?
It's like calling preventDefault and stopPropagation at once.
Unfortunately, nothing comes up before your event.
Please non working jsfiddle
example AND working jsfiddle
example Try changing above code to
$('a[href*=#]:not([href=#])').click(function(e) {
    if (location.pathname.replace(/^\//, '') === this.pathname.replace(/^\//, '') && location.hostname === this.hostname) {

      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top
        }, 1000);
        e.preventDefault();
      }
    }
  });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question