Tony Lee Tony Lee - 1 year ago 74
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>
<link rel=stylesheet id="style" href='style.css'/>
<script src="phone.js"></script>

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 Source

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

        console.log( "Is phone: " + phone() );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download