100% Javascript html file works on Windows Chrome but not on Mac Chrome browsers.

The attached  javascript code works on W3Schools using a  Windows browser  but not on Mac Chrome or Safari browser. I started by using the Try it example for the slice operator. I inserted some code from a routine that returns CST from (Central Standard Time). It works when I do the exercise inside w3schools on a Windows browser. When I repeat the same process using w3schools on a Mac Chrome browser it doesn't. It complains when encountering the slice operator.
<!DOCTYPE html>
<html>
<body>

<p>Click the button to extract the second and the third elements from the array.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
            var x = new Date();
            var c = x.toString();
            var tz = " ";
            if (c.indexOf("(") > 0) {
                var e = c.slice(c.indexOf("(") + 1, c.indexOf(")"));
                var f = e.split(' ');
                f[0] = f[0].slice(0, 1);
                f[1] = f[1].slice(0, 1);
                f[2] = f[2].slice(0, 1);
                tz = f[0] + f[1] + f[2];
            } else {
                var f = c.split(' ');
                tz = f[4];
            }
    document.getElementById("demo").innerHTML = tz;
}
</script>

</body>
</html>

Open in new window

ROBERT MECHLERProgramming managerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
ROBERT MECHLERProgramming managerAuthor Commented:
Never tried to make something work on a Mac website before. Didn't realize the new Date() in javascript produced a different length string. The following works on both a Windows and Mac
<!DOCTYPE html>
<html>
<body>

<p>Click the button to extract the second and the third elements from the array.</p>

<button onclick="myFunction()">Try it</button>

<p id="len"></p>
<p id="demo"></p>
<p id="ftimezone"></p>
<p id="timezone"></p>

<script>
function myFunction() {
            var x = new Date();
            var c = x.toString();
            document.getElementById("len").innerHTML = c.length;
            document.getElementById("demo").innerHTML = c;
            var e = c.slice(c.indexOf("(") + 1, c.indexOf(")"));
            document.getElementById("ftimezone").innerHTML = e;
            if(e.length == 3){
                document.getElementById("timezone").innerHTML = e;            	
            }else{
                var f = e.split(' ');
                f[0] = f[0].slice(0, 1);
                f[1] = f[1].slice(0, 1);
                f[2] = f[2].slice(0, 1);
                var tz = f[0] + f[1] + f[2];
                 document.getElementById("ftimezone").innerHTML = tz;                                   
                 }

}
</script>
</body>
</html>

Open in new window

2
All Courses

From novice to tech pro — start learning today.