Home >Web Front-end >JS Tutorial >How Can I Reliably Detect Internet Explorer Usage for Targeted Function Execution?
When interacting with users through web interfaces, it may be necessary to differentiate between browsers for targeted functionality. Specifically, users may encounter scenarios where executing a function exclusively for Internet Explorer (IE) users is desirable.
One method to determine if a user is employing IE is to inspect the browser's User Agent string. However, this approach has become more complex over time due to changes in Edge's rendering engine.
To check for IE or Edge use, a comprehensive approach is recommended:
if (window.document.documentMode) { // Do IE stuff }
This check will yield positive results for both IE and Edge.
For a more specific detection of IE 11, the following function can be utilized:
function detectIEEdge() { var ua = window.navigator.userAgent; var msie = ua.indexOf('MSIE '); var trident = ua.indexOf('Trident/'); var edge = ua.indexOf('Edge/'); if (msie > 0 || trident > 0) { return true; } else if (edge > 0) { return true; } else { return false; } }
By incorporating these methods, you can effectively control the execution of functions based on the user's browser, ensuring targeted functionality specifically for IE users.
The above is the detailed content of How Can I Reliably Detect Internet Explorer Usage for Targeted Function Execution?. For more information, please follow other related articles on the PHP Chinese website!