JavaScript基础
BOM基础
重要程度:8 分
<h1>浏览器对象模型 (BOM) 基础</h1>
<p><strong>1. Window 对象</strong></p>
<ul>
<li>Window 对象是浏览器对象模型的根对象。</li>
<li>它代表浏览器窗口,并且提供了对浏览器窗口的属性、方法和事件的访问。</li>
</ul>
<p><strong>2. Location 对象</strong></p>
<ul>
<li>Location 对象包含有关当前 URL 的信息。</li>
<li>通过 window.location 或 location 访问。</li>
<li>常用属性:href、protocol、hostname、port、pathname、search。</li>
</ul>
<p><strong>3. Navigator 对象</strong></p>
<ul>
<li>Navigator 对象包含有关浏览器的信息。</li>
<li>通过 window.navigator 或 navigator 访问。</li>
<li>常用属性:appVersion、appName、platform、userAgent。</li>
</ul>
<p><strong>4. History 对象</strong></p>
<ul>
<li>History 对象包含用户在浏览器中访问过的 URL。</li>
<li>通过 window.history 或 history 访问。</li>
<li>常用方法:back()、forward()、go()。</li>
</ul>
<h2>例题解析</h2>
<p><strong>例题 1:</strong> 在网页中显示当前页面的完整 URL。</p>
<pre>
<script>
document.write("当前页面的完整URL是: " + window.location.href);
</script>
</pre>
<p><strong>例题 2:</strong> 使用 History 对象后退到上一个页面。</p>
<pre>
<button onclick="history.back()">返回上一页</button>
</pre>
<p><strong>例题 3:</strong> 获取浏览器的名称。</p>
<pre>
<script>
document.write("当前使用的浏览器是: " + navigator.appName);
</script>
</pre>