在页面加载之前运行JavaScript函数(设置适当大小的背景) [英] Running JavaScript function before page load (setting background of appropriate size)

查看:135
本文介绍了在页面加载之前运行JavaScript函数(设置适当大小的背景)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像背景,无论用户的分辨率如何,我希望它始终可见。因此,我希望能够在开始之前确定分辨率是什么,并在页面加载之前设置适当的背景图像文件。有可能吗?

I've got an image background which content I want to be always visible, no matter what is the user's resolution. Therefore, I want to be able to determine what is the resolution and set appropriate background image file before page loads, on the very beginning. Is it possible somehow?

推荐答案

最早可以运行Javascript函数来访问DOM(无需等待页面加载)是在打开< body> 标记后立即放置< script> 标记。

The earliest point at which you can run a Javascript function with access to the DOM (without waiting for page load) is by placing a <script> tag right after the opening <body> tag.

< head> 内的脚本会在此之前运行,但是赢了无法访问页面上的元素。

Scripts inside the <head> will run before this occurs, but there won't be access to the elements on the page.

试用此代码

<body>
    <script type="text/javascript">
        alert("Some elements shouldn't even be visible when this shows");
    </script>
    ... rest of the page
</body>

请参阅示例,包含一些 Gangsta Lipsum 文字。

See this example with some Gangsta Lipsum text.

这篇关于在页面加载之前运行JavaScript函数(设置适当大小的背景)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆