Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hiding JS from IE/mac and Safari

    Hi, I have already searched the forum and Google, to no avail. So Im posting a new thread.

    I have a script which causes some bugs in safari and IE/mac. I would like to start the script with
    if !mac....
    etc, but have been unable to find a statement which doesn't cause issues in other browsers.
    Can someone please help?
    The script is external, serves no presentational purpose and would not be noticed if removed. But I would still like to serve it to PC's.
    Thanks.
    Andrew

    I take no responsibility for the above nonsense.


    Left Justified

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    There are many browser sniffer scripts on the net but probably the better solution is via object detection. That depends on what javascript codes that were not working for mac. Can we see some snippets of the codes?

  • #3
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why certainly
    Here's the js
    Code:
    /* Smooth scrolling
       Changes links that link to other parts of this page to scroll
       smoothly to those links rather than jump to them directly, which
       can be a little disorienting.
       
       sil, http://www.kryogenix.org/
       
       v1.0 2003-11-11
    */
    
    function ss_fixAllLinks() {
      // Get a list of all links in the page
      var allLinks = document.getElementsByTagName('a');
      // Walk through the list
      for (var i=0;i<allLinks.length;i++) {
        var lnk = allLinks[i];
        if ((lnk.href && lnk.href.indexOf('#') != -1) && 
            ( (lnk.pathname == location.pathname) ||
    	  ('/'+lnk.pathname == location.pathname) ) && 
            (lnk.search == location.search)) {
          // If the link is internal to the page (begins in #)
          // then attach the smoothScroll function as an onclick
          // event handler
          ss_addEvent(lnk,'click',smoothScroll);
        }
      }
    }
    
    function smoothScroll(e) {
      // This is an event handler; get the clicked on element,
      // in a cross-browser fashion
      if (window.event) {
        target = window.event.srcElement;
      } else if (e) {
        target = e.target;
      } else return;
      
      // Make sure that the target is an element, not a text node
      // within an element
      if (target.nodeType == 3) {
        target = target.parentNode;
      }
      
      // Paranoia; check this is an A tag
      if (target.nodeName.toLowerCase() != 'a') return;
      
      // Find the <a name> tag corresponding to this href
      // First strip off the hash (first character)
      anchor = target.hash.substr(1);
      // Now loop all A tags until we find one with that name
      var allLinks = document.getElementsByTagName('a');
      var destinationLink = null;
      for (var i=0;i<allLinks.length;i++) {
        var lnk = allLinks[i];
        if (lnk.name && (lnk.name == anchor)) {
          destinationLink = lnk;
          break;
        }
      }
      
      // If we didn't find a destination, give up and let the browser do
      // its thing
      if (!destinationLink) return true;
      
      // Find the destination's position
      var destx = destinationLink.offsetLeft; 
      var desty = destinationLink.offsetTop;
      var thisNode = destinationLink;
      while (thisNode.offsetParent && 
            (thisNode.offsetParent != document.body)) {
        thisNode = thisNode.offsetParent;
        destx += thisNode.offsetLeft;
        desty += thisNode.offsetTop;
      }
      
      // Stop any current scrolling
      clearInterval(ss_INTERVAL);
      
      cypos = ss_getCurrentYPos();
      
      ss_stepsize = parseInt((desty-cypos)/ss_STEPS);
      ss_INTERVAL = setInterval('ss_scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10);
      
      // And stop the actual click happening
      if (window.event) {
        window.event.cancelBubble = true;
        window.event.returnValue = false;
      }
      if (e && e.preventDefault && e.stopPropagation) {
        e.preventDefault();
        e.stopPropagation();
      }
    }
    
    function ss_scrollWindow(scramount,dest,anchor) {
      wascypos = ss_getCurrentYPos();
      isAbove = (wascypos < dest);
      window.scrollTo(0,wascypos + scramount);
      iscypos = ss_getCurrentYPos();
      isAboveNow = (iscypos < dest);
      if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
        // if we've just scrolled past the destination, or
        // we haven't moved from the last scroll (i.e., we're at the
        // bottom of the page) then scroll exactly to the link
        window.scrollTo(0,dest);
        // cancel the repeating timer
        clearInterval(ss_INTERVAL);
        // and jump to the link directly so the URL's right
        location.hash = anchor;
      }
    }
    
    function ss_getCurrentYPos() {
      if (document.body && document.body.scrollTop)
        return document.body.scrollTop;
      if (document.documentElement && document.documentElement.scrollTop)
        return document.documentElement.scrollTop;
      if (window.pageYOffset)
        return window.pageYOffset;
      return 0;
    }
    
    function ss_addEvent(elm, evType, fn, useCapture)
    // addEvent and removeEvent
    // cross-browser event handling for IE5+,  NS6 and Mozilla
    // By Scott Andrew
    {
      if (elm.addEventListener){
        elm.addEventListener(evType, fn, useCapture);
        return true;
      } else if (elm.attachEvent){
        var r = elm.attachEvent("on"+evType, fn);
        return r;
      } else {
        alert("Handler could not be removed");
      }
    } 
    
    var ss_INTERVAL;
    var ss_STEPS = 25;
    
    ss_addEvent(window,"load",ss_fixAllLinks);
    And it's being applied to this page .
    Thankyou very much.
    Andrew

    I take no responsibility for the above nonsense.


    Left Justified

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts

    The code is quite long

    Which parts of the codes are not working with mac?

  • #5
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I cannot remember the exact wording of the error message as I only had very little access to a mac for testing, and wanted to test all the browsers it had installed in that time.
    Although the message did not state that it was directly a JS issue, the page did not encounter such an error previous to the inclusion of the script.
    As I don't have access to a mac to further investigate, I would prefer to merely hide the script entirely as it is only for decoration. Plus it's for college and will only be checked in IE and Firebird. (HOORAY!!! My nagging worked and I returned this year to find Firebird on all the systems.)

    Anyway, thank you greatly for your help, if you could provide me with a other-browser-friendly "if (!mac) " statement, that would be awesome

    Andrew

    I take no responsibility for the above nonsense.


    Left Justified

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    /mac/i.test(navigator.platform)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by liorean
    /mac/i.test(navigator.platform)
    I'm sorry David, I have no clue how to implement that.
    I decided early on that I wouldn't learn .js until I had a firm understanding of XHTML/CSS, and I'm not there yet

    As you've prob. work out, they functions aren't being specifically called by the html, all internal <a>'s are being affected. (As intended).
    So how on earth do I rap the script in a "No mac past this point" fence?
    So many languages to learn...so in need of sleep......

    Thanks guys,
    Andrew

    I take no responsibility for the above nonsense.


    Left Justified

  • #8
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Liorean: I hit google for the code snippet you gave me, and it returned two posts by you on [thelist] and your Theme Switcher Tute only.
    I did find this amongst it though, could I use this?
    If so, how?
    Code:
    if(navigator.platform.toLowerCase().indexOf('mac') != -1)
    If my understanding is correct, (and it rarely is in .js) removing the '!' will make it exclude mac instead of selecting it?
    I feel like a fool having to ask so many questions about this.

    I take no responsibility for the above nonsense.


    Left Justified

  • #9
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Not exactly. != means NOT EQUAL. == mean EQUAL. = means ASSIGNMENT. ! means NOT.

    /mac/i.test(navigator.platform) is an expression that returns a boolean. It will be true if the platform is mac, it will be false if not. You use it like this:
    Code:
    if(/mac/i.test(navigator.platform))
        /* things to do if you have a mac */
    
    
    if(!/mac/i.test(navigator.platform))
        /* things to do if you don't have a mac */
    
    
    // or store it in a variable:
    var
        mac=/mac/i.test(navigator.platform);
    
    if(mac)
        /* things to do if you have a mac */
    
    
    if(!mac)
        /* things to do if you don't have a mac */
    Does that clear it up?
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #10
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A concise response as always, thankyou greatly

    Lastly, would wrapping the entire set of functions in
    Code:
    if(!/mac/i.test(navigator.platform)
    achieve what I'm after?
    My train of thought leads me to believe that each function would have to contain its own anti-mac script immedietly following the function call.

    Also, does .js require that all if () statements also have an else()?
    Andrew

    I take no responsibility for the above nonsense.


    Left Justified

  • #11
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Don't enclose the function declarations in the if statement. Only enclose the code that causes them to be called.

    And no, there is no need for including an else if you don't need it.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #12
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by liorean
    Don't enclose the function declarations in the if statement. Only enclose the code that causes them to be called.
    Ahh, and there is the problem... The script attaches itself to all <a>'s who's href starts with '#'. The only reference to the script is <script src=""...> in the head. Perhaps a parsing bug similar to what we use to hide CSS? Surely that would invalidate my code, no?

    I take no responsibility for the above nonsense.


    Left Justified

  • #13
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Oh, I forgot you're not used to looking for these. Here's ther single call that starts it all: ss_addEvent(window,"load",ss_fixAllLinks); If you change that to:
    Code:
    if(!/mac/i.test(navigator.platform))
        ss_addEvent(window,"load",ss_fixAllLinks);
    it will all be ignored in mac browsers.
    Last edited by liorean; 02-07-2004 at 12:42 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #14
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Awesome, thanks David
    Now it's time for me to go back to my little CSS corner and leave .js to people who know how to use it ...

    I take no responsibility for the above nonsense.


    Left Justified


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •