0

The following smooth scrolling script messes up my navigation in Safari (anchor tags don't work anymore). I'm a Javascript newbie, could anyone tell me how to detect Safari in this script and prevent it from executing when it detects Safari? Many thanks!

// JavaScript Document
$(document).ready(function() {
  function filterPath(string) {
  return string
    .replace(/^\//,'')
    .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
    .replace(/\/$/,'');
  }
  var locationPath = filterPath(location.pathname);
  var scrollElem = scrollableElement('html', 'body');

  $('a[href*=#]').each(function() {
    var thisPath = filterPath(this.pathname) || locationPath;
    if (  locationPath == thisPath
    && (location.hostname == this.hostname || !this.hostname)
    && this.hash.replace(/#/,'') ) {
      var $target = $(this.hash), target = this.hash;
      if (target) {
        var targetOffset = $target.offset().top;
        $(this).click(function(event) {
          event.preventDefault();
          $(scrollElem).animate({scrollTop: targetOffset}, 1300, function() { // scroll speed
            location.hash = target;
          });
        });
      }
    }
  });

  // use the first element that is "scrollable"
  function scrollableElement(els) {
    for (var i = 0, argLength = arguments.length; i <argLength; i++) {
      var el = arguments[i],
          $scrollElement = $(el);
      if ($scrollElement.scrollTop()> 0) {
        return el;
      } else {
        $scrollElement.scrollTop(1);
        var isScrollable = $scrollElement.scrollTop()> 0;
        $scrollElement.scrollTop(0);
        if (isScrollable) {
          return el;
        }
      }
    }
    return [];
  }

});
Stefaan
  • 184
  • 4
  • 6
  • 16

3 Answers3

0

Refer: http://api.jquery.com/jQuery.browser/, to detect browser and execute script only if it is not safari

Kalpesh Patel
  • 2,772
  • 2
  • 25
  • 52
  • from the docs: `$.browser.safari` is deprecated, so this solution is not for out-of-the-box use – gion_13 Oct 12 '12 at 09:43
0
var isSafari = navigator.userAgent.indexOf("Safari") > -1 && navigator.userAgent.indexOf("Chrome") == -1;
if(!isSafari){
    // do your magic
}
gion_13
  • 41,171
  • 10
  • 96
  • 108
  • OR May be OP should look into the full link.....http://stackoverflow.com/questions/5899783/detect-safari-using-jquery – Raab Oct 12 '12 at 09:45
  • Comment deleted, haven't got safari ATM, so I wasn't sure about the `userAgent` value, that's why I suggested `navigator.vendor`. Either way should work – Elias Van Ootegem Oct 12 '12 at 10:25
0

Perhaps this:

$(document).ready(function()
{
    if (navigator.appVersion.match(/WebKit/) && !navigator.vendor.match(/Google/))
    {
        return;
    }
    //rest of your code
});

if you have any code outside the $(document).ready callback, you could kill the script by throwing an error, by writing this at the top of your script(s):

if (navigator.appVersion.match(/WebKit/) && !navigator.vendor.match(/Google/))
{
    throw new Error('Safari not supported');
}
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149