Tony Lee Tony Lee - 1 month ago 7
Javascript Question

JavaScript function in an external file won't run



'use strict';
function phone() {
if( navigator.userAgent.match(/Android/gi)
|| navigator.userAgent.match(/webOS/gi)
|| navigator.userAgent.match(/iPhone/gi)
|| navigator.userAgent.match(/iPad/gi)
|| navigator.userAgent.match(/iPod/gi)
|| navigator.userAgent.match(/BlackBerry/gi)
|| navigator.userAgent.match(/Windows Phone/gi)
){
return true;
}
else {
return false;
}
}

<!doctype html>
<html>
<head>
<link rel=stylesheet id="style" href='style.css'/>
</head>
<body>
<script src="phone.js"></script>
<script>
phone();
</script>
</body>
</html>





The external function won't run when the page opens why is that? I even tried doing onload but nothing happened. When the page opens it should print in the console either true or false but absolutely nothing is printed.

Answer

You are not actually printing the result. You need to change to this:

    <script>
        console.log( "Is phone: " + phone() );
    </script>