How can the issue of compatibility with mobile devices be addressed when using JavaScript functions like window.open()?

The issue of compatibility with mobile devices when using JavaScript functions like window.open() can be addressed by using feature detection to check if the device is a mobile device before executing the function. This can be done by checking the user agent string or using a library like Modernizr to detect mobile devices.

<?php
$user_agent = $_SERVER['HTTP_USER_AGENT'];

if (strpos($user_agent, 'Mobile') !== false) {
    // Code to open new window for mobile devices
    echo '<script>window.open("https://example.com");</script>';
} else {
    // Code for non-mobile devices
    echo '<script>alert("This feature is only available on mobile devices.");</script>';
}
?>