Webbrowser, detect if there is a PDF reader instal

2019-02-10 04:39发布

问题:

Is there a way to check if the user has installed a PDF reader? Maybe with a ActiveX component?

Could you please help me?

回答1:

You can detect installed plugins with window.navigator.plugins This will return an array with details of installed plugins, but this will not work for Internet Explorer. When running this code, IE only returns an array with embed tags in the page. Thanks IE, so useful?

Ok, let's try to work this out. The following function should work in all major browsers expect IE.

function hasPlugin(name) {
    name = name.toLowerCase();
    var plugins = window.navigator.plugins;
    for (var i=0, len = plugins.length; i < len; i++) {
        if (plugins[i].name.toLowerCase().indexOf(name) > -1) {
            return true;
        }
    }
    return false;
}

you can call this function and check the plugin status like this

hasPlugin('Flash');
hasPlugin('QuickTime');

For IE, we should try this

function hasPlugin(name) {
    try {
        new ActiveXObject(name);
        return true;
    } catch (e) {
        return false;
    }
}

you can call this function and check the plugin status for IE

hasPlugin('ShockwaveFlash.ShockwaveFlash');

You can made this function declaration cross browser like this

var hasPlugin;
if (navigator.userAgent.indexOf('MSIE')) {
    hasPlugin = function(name) {
        try {
            new ActiveXObject(name);
            return true;
        } catch (e) {
            return false;
        }
    }
}
else {
    hasPlugin = function(name) {
        name = name.toLowerCase();
        var plugins = window.navigator.plugins;
        for (var i=0, len = plugins.length; i < len; i++) {
            if (plugins[i].name.toLowerCase().indexOf(name) > -1) {
                return true;
            }
        }
        return false;
    }
}

Then you can call the function in a cross browser way. I am sorry, I don't installed any PDF plugin for my browsers, -Firefox, Chrome or IE- so I could tell you exact name the argument we should pass hasPlugin function.

I hope, this will help you. By the way, I did not tried the code in browsers, this is a therocial knowledge on me. But I guess this will help you -hope- :-)



回答2:

No, I don't think so - but you could always direct your links through Google's PDF reader by default - which will work for everyone.

http://docs.google.com/viewer

Please be aware that this will channel your PDF files through Google's servers, so you will lose an element of security.



回答3:

This was very useful for me:

Java script - Adobe plug-in detector

From comments area, get the corrections for Safari Browser too.