Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript HTML-如何在不同的用户代理中显示不同的图像?_Javascript_Html_User Agent - Fatal编程技术网

Javascript HTML-如何在不同的用户代理中显示不同的图像?

Javascript HTML-如何在不同的用户代理中显示不同的图像?,javascript,html,user-agent,Javascript,Html,User Agent,如果我使用Chrome,我想显示img1.jpg()。否则,如果我使用IE,我想显示img2.jpg()。可能吗 提前感谢。您可以使用下面的函数完成此操作,然后只需使用IF语句设置该图像的属性 function browsercheck() { var userAgent = window.navigator.userAgent; return (userAgent.indexOf("MSIE ") > 0 || !! userAgent.match(/Trident.*r

如果我使用Chrome,我想显示img1.jpg(
)。否则,如果我使用IE,我想显示img2.jpg(
)。可能吗


提前感谢。

您可以使用下面的函数完成此操作,然后只需使用IF语句设置该图像的属性

function browsercheck() {
    var userAgent = window.navigator.userAgent;
    return (userAgent.indexOf("MSIE ") > 0 || !! userAgent.match(/Trident.*rv\:11\./));

}

var browser = browsercheck() ? 'IE' : 'Chrome';

if (browser == "IE")
{
     //Set attribute   
}

else if (browser == "Chrome")
{
     //Set attribute   
}
如果您需要了解确切的浏览器,请使用下面的内容

navigator.browsercheck= (function(){
    var ua= navigator.userAgent, tem,
    M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
    if(/trident/i.test(M[1])){
        tem=  /\brv[ :]+(\d+)/g.exec(ua) || [];
        return 'IE '+(tem[1] || '');
    }
    if(M[1]=== 'Chrome'){
        tem= ua.match(/\b(OPR|Edge)\/(\d+)/);
        if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
    }
    M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
    if((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
    return { 'browser': M[0], 'version': M[1] };
})();
然后,您可以使用
navigator.browserInfo.browser
查询此信息以获取浏览器信息,或者如果您需要知道确切的版本,可以使用
navigator.browserInfo.version

学分:

匹配[1]给出浏览器名称。“rv:11”也表示IE11

例如,您可以准备带前缀的图像并使用匹配的值作为前缀:

myImg.src=match[1]+"-img1.jpg";
myImg.alt=match[1]+" Image";

是的。可能重复的请参见编辑以获取准确的浏览器信息。看起来他们是在追求Chrome或IE,但对于确切的版本,他们可以加入添加的代码
myImg.src=match[1]+"-img1.jpg";
myImg.alt=match[1]+" Image";