1

I using this script of code where I am detecting OS of user and if it is a mobile OS then I am displaying another window.

function getOS() {
    var userAgent = window.navigator.userAgent,
    platform = window.navigator?.userAgentData?.platform || window.navigator.platform,
    macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K','iPad'],
    windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
    iosPlatforms = ['iPhone', 'iPod'],
    os = null;
    
    if (macosPlatforms.indexOf(platform) !== -1) {

    } else if (iosPlatforms.indexOf(platform) !== -1) {
        location.replace("mobile.html")
    } else if (windowsPlatforms.indexOf(platform) !== -1) {
  
    } else if (/Android/.test(userAgent)) {
        location.replace("mobile.html")
    } else if (/Linux/.test(platform)) {

    }        
    return os;
}
getOS()

This script is in the start of document inside the head tag. when I am running the hosted website in mobile. It is showing a dialog box that shows the text " Do you want to allow popups from this page". I think this is because of the fact that I am opening a different window. I tried using window.open() function instead of location.replace() because I thought that would help but it is of no use. Can anyone help me figure out how to not show that dialog at default because it is ruining overall website experience.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109

1 Answers1

1

I would set location.href rather than using replace. You can use a relative URL when setting the href. See Redirecting to a relative URL in JavaScript for more details.

function getOS() {
    var userAgent = window.navigator.userAgent,
    platform = window.navigator?.userAgentData?.platform || window.navigator.platform,
    macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K','iPad'],
    windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
    iosPlatforms = ['iPhone', 'iPod'],
    os = null;
    
    if (macosPlatforms.indexOf(platform) !== -1) {

    } else if (iosPlatforms.indexOf(platform) !== -1) {
        location.href = "/mobile.html"
    } else if (windowsPlatforms.indexOf(platform) !== -1) {
  
    } else if (/Android/.test(userAgent)) {
        location.href = "/mobile.html"
    } else if (/Linux/.test(platform)) {

    }        
    return os;
}
getOS()
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109