<p>window.location 对象不仅可以获得当前页面的地址 (URL),还能够将浏览器重定向到新的页面。</p><p><strong>1、window.location.pathname</strong></p><p>设置或获取对象指定的文件名或路径。</p><p>例:</p><pre class="brush:js;toolbar:false">http://panxu.net/index?id=361 console.log(window.location.pathname);</pre><p>则输出:</p><pre class="brush:html;toolbar:false">/index</pre><p><strong>2、window.location.href</strong></p><p>设置或获取整个 URL 为字符串。</p><p>例:</p><pre class="brush:js;toolbar:false">http://panxu.net/index?id=361 console.log(window.location.href);</pre><p>则输出:</p><pre class="brush:html;toolbar:false">http://panxu.net/index?id=361</pre><p><strong>3、window.location.port</strong></p><p>设置或获取与 URL 关联的端口号码。</p><p>例:</p><pre class="brush:js;toolbar:false">http://panxu.net/index?id=361 console.log(window.location.port);</pre><p>则输出:</p><pre class="brush:html;toolbar:false">8086</pre><p><strong>4、window.location.protocol</strong></p><p>返回使用的 web 协议(http: 或 https:)</p><p>例:</p><pre class="brush:js;toolbar:false">http://panxu.net/index?id=361 console.log(window.location.protocol);</pre><p>则输出:</p><pre class="brush:html;toolbar:false">http:</pre><p><strong>5、window.location.hash</strong></p><p>设置或获取 href 属性中在井号“#”后面的分段。</p><p><strong>6、window.location.host</strong></p><p>设置或获取 location 或 URL 的 hostname 和 port 号码。</p><p>例:</p><pre class="brush:js;toolbar:false">http://panxu.net:8086/index?id=361 console.log(window.location.host);</pre><p>则输出:</p><pre class="brush:html;toolbar:false">http://panxu.net:8086</pre><p><strong>7、window.location.search</strong></p><p>设置或获取 href 属性中跟在问号后面的部分。</p><p>例:</p><pre class="brush:js;toolbar:false">http://panxu.net/index?id=361 console.log(window.location.search);</pre><p>则输出:</p><pre class="brush:html;toolbar:false">?id=361</pre><p>以上就是关于“js或jQuery获取页面url,获取url地址”的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流。</p>