简体   繁体   English

为什么此代码在IE和Safari中不起作用?

[英]Why does this code not work in IE & Safari?

I've been tasked with writing some code, that places a small icon in the top right corner of an Ad regardless of size. 我的任务是编写一些代码,无论尺寸大小,该代码都会在广告的右上角放置一个小图标。 Depending on the language of the user (in this case I'm using the browser language) it will change the icon (based on a bank of images on a server somewhere) and will subsequently change the URL of the click through for that image. 根据用户的语言(在这种情况下,我使用的是浏览器语言),它将更改图标(基于某处服务器上的图像库),并随后更改该图像的点击URL。 I know that I can get this to work by reading the OS language with the HTTP header, but I can't do this and it needs to be done server side. 我知道我可以通过使用HTTP标头读取OS语言来使其工作,但是我无法做到这一点,它需要在服务器端完成。 The script was written by me but the HTML was inherited by someone else - I also have the ability to edit both. 脚本是我写的,但HTML是别人继承的-我也可以同时编辑两者。

Currently the code works in Firefox, Chrome and Opera but will not work when I make the default browser language another European language (say French) in IE and Safari..!! 目前,该代码可在Firefox,Chrome和Opera中运行,但是当我在IE和Safari中将默认浏览器语言设为另一种欧洲语言(例如法语)时,该代码将无法工作。 This is driving me mad now so if you can help me I'd be so grateful. 现在这使我发疯,所以如果您能帮助我,我将非常感激。 On some advice, there is a live example here: 根据一些建议,这里有一个实时示例:

Live demo: http://jsfiddle.net/Juu7t/ 现场演示: http //jsfiddle.net/Juu7t/

EDIT: Right so after playing around with the code, I still can't see why it doesn't work. 编辑:正确,所以在玩完代码后,我仍然看不到为什么它不起作用。 What is essentially happening is the default image will load (for IE and Safari) but it won't swap the image based on the browser language. 本质上发生的是将加载默认图像(适用于IE和Safari),但不会基于浏览器语言交换图像。 The JavaScript console doesn't show any errors and Firebug isn't helping me! JavaScript控制台未显示任何错误,Firebug并没有帮助我!

EDIT2: Right so I wrote this small function to test whether Internet Explorer is actually reading the language correctly. EDIT2:是的,所以我写了这个小函数来测试Internet Explorer是否实际上正在正确读取该语言。 (I'm using IE9): (我正在使用IE9):

function checkIE () {
if ((navigator.browserLanguage) || (window.navigator.language)) {
var testbrowser = navigator.browserLanguage || window.navigator.language;
alert(testbrowser);
}   
}
checkIE();

Even when I change the language in IE to "French" ie "fr" it still returns "en-US"!! 即使我将IE中的语言更改为“法语”(即“ fr”),它仍会返回“ en-US”! am I doing something wrong or am I reading the wrong value from the navigator object?! 我做错了什么还是从导航器对象中读取了错误的值?

I am also looking for solution and I found the solution in a post as 我也在寻找解决方案,我在帖子中找到了解决方案

$.ajax({ 
  url: "http://ajaxhttpheaders.appspot.com", 
  dataType: 'jsonp', 
  success: function(headers) {
    language = headers['Accept-Language'];
    nowDoSomethingWithIt(language);
  }
});

You can see the complete details here 您可以在此处查看完整的详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么此代码在Safari中有效但在Chrome中不起作用? Arrrgh - Why does this code work in Safari but not Chrome? Arrrgh 为什么我的代码在Safari或Opera中不起作用? - Why does my code not work in Safari or Opera? 为什么Safari / Opera无法与此JavaScript代码一起使用? - Why does Safari/Opera not work with this javascript code? JS代码使IE9和Safari冻结,在Opera中不起作用? - JS code makes IE9 and Safari freezing and does not work in Opera? 为什么这个jQuery代码可以在Firefox中运行,但不适用于Chrome或Safari? - Why does this jQuery code work in Firefox but not Chrome or Safari? 为什么此javascript在IE7或Safari中不起作用? - Why would this javascript not work in IE7 or Safari? 为什么这在FireFox上有效但在Safari上无效? - Why does this work on FireFox but not on Safari? 为什么以下代码在safari中起作用,但在IE6中不起作用? - why the following code works in safari but not in IE6? 为什么此代码在FireFox和IE中不能使用,而在Chrome中不能使用? - Why Doesn't This Code Work in FireFox and IE but does in Chrome? 为什么此jQuery代码在Chrome和Firefox中能正常工作,而在IE9上却不能工作? - Why does this jQuery code work fine in Chrome and Firefox, but not IE9?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM