简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM