如何将 screen.width 值替换为我自己定义的值

How to replace screen.width value with my own defined value?

本文关键字:我自己 定义 替换 screen width      更新时间:2023-09-26

如何将screen.width值替换为我自己定义的值?

我想定义一个假screen.width

var screen.width = 1024;

<head>

这样当下面的代码加载时,它将始终在任何screen.width中显示 = <h1>test 2</h1>

我正在使用以下代码:

<script type="text/javascript">
if (screen.width>=1400)
{
    document.write('<h1>test 1</h1>');
}else 
{
    document.write('<h1>test 2</h1>');
}
</script>

希望我能清楚地解释我的问题。

只是为了好玩,你可以像这样使screen的属性可变:

var tempScreen = {};
for (var obj in screen)
  tempScreen[obj] = screen[obj];
tempScreen.__proto__ = screen.__proto__;
screen = tempScreen;
screen.width = 400;
console.log(screen.width);  // -> 400