...

View Full Version : Hiding JS from IE/mac and Safari



mindlessLemming
02-06-2004, 02:28 AM
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

glenngv
02-06-2004, 06:00 AM
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?

mindlessLemming
02-06-2004, 09:42 AM
Why certainly :D
Here's the js


/* 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 (http://studentwork.qantm.com.au/akrespanis/NavAust/index.html) .
Thankyou very much.
Andrew:)

glenngv
02-06-2004, 10:51 AM
Which parts of the codes are not working with mac?

mindlessLemming
02-06-2004, 02:47 PM
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.:D:cool::D)

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

Andrew

liorean
02-06-2004, 03:03 PM
/mac/i.test(navigator.platform)

mindlessLemming
02-06-2004, 04:42 PM
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

mindlessLemming
02-06-2004, 04:52 PM
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?

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.

liorean
02-06-2004, 05:10 PM
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:
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?

mindlessLemming
02-07-2004, 12:39 AM
A concise response as always, thankyou greatly :)

Lastly, would wrapping the entire set of functions in

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

liorean
02-07-2004, 12:49 AM
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.

mindlessLemming
02-07-2004, 01:06 AM
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?

liorean
02-07-2004, 01:09 AM
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:
if(!/mac/i.test(navigator.platform))
ss_addEvent(window,"load",ss_fixAllLinks);it will all be ignored in mac browsers.

mindlessLemming
02-07-2004, 01:32 AM
Awesome, thanks David :thumbsup:
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 ...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum