【发布时间】:2011-08-29 22:45:24
【问题描述】:
好吧,我通常尽量不问问题,我已经坚持了 20 多个小时...这种烂...
无论如何,我正在尝试创建一个检测浏览器分辨率的 JavaScript,并根据浏览器分辨率比较语句为页面重新加载选择最合适的 CSS。我知道它更容易使用百分比,但是对于这个项目包含的内容,这是无法做到的。所以我一直在写一些基本的东西,并且由于某种迟钝的原因似乎找不到错误......
在比较语句之前一切都很顺利。
这是脚本:
</head>
<script type="text/javascript">
function smallTest() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
}
if ((myWidth == 1280) && (myHeight == 768))
{document.write("<link href='css/Foo.1280.800.css' rel='stylesheet' type='text/css'>");}
else if (( myWidth == 800) && ( myHeight == 600))
{ document.write("<link href='css/Foo.1024.768.css' rel='stylesheet' type='text/css'>");}
else if (( myWidth == 1024) && ( myHeight == 768))
{ document.write("<link href='css/Foo.1024.768.css' rel='stylesheet' type='text/css'>");}
else if ( (myWidth == 1280) && ( myHeight == 1040))
{document.write("<link href='css/Foo.1280.1040.css' rel='stylesheet' type='text/css'>");}
else if (( myWidthh == 1600) && ( myHeight == 900))
{document.write("<link href='css/Foo.1600.900.css' rel='stylesheet' type='text/css'>");}
else if (( myWidth == 1920) && ( myHeight == 1080))
{document.write("<link href='css/Foo.1920.1080.css' rel='stylesheet' type='text/css'>");}
else if ( (myWidth == 1920) && ( myHeight == 1200))
{ document.write("<link href='css/Foo.1920.1200.css' rel='stylesheet' type='text/css'>");}
</script>
<body window.onload="smallTest()">
【问题讨论】:
-
条件以某种方式阻止了 CSS 加载。 idk y
-
这是一个媒体查询的工作。
标签: javascript css javascript-events browser-detection