获取当前地址 JS获取当前URL

本文详细解析了如何获取网页的URL、当前页面路径、顶级页面路径、主机名等关键信息,并展示了如何从URL中提取页面名称。通过脚本演示,帮助开发者更好地理解并操作网页元信息。

 <table width=100% cellpadding=0 cellspacing=0 border=0 >
<scrīpt> 
thisURL = document.URL; 
thisHREF = document.location.href; 
thisSLoc = self.location.href; 
thisDLoc = document.location; 
strwrite = "<tr><td valign=top>thisURL: </td><td>[" + thisURL + "]</td></tr>" 
strwrite += "<tr><td valign=top>thisHREF: </td><td>[" + thisHREF + "]</td></tr>" 
strwrite += "<tr><td valign=top>thisSLoc: </td><td>[" + thisSLoc + "]</td></tr>" 
strwrite += "<tr><td valign=top>thisDLoc: </td><td>[" + thisDLoc + "]</td></tr>" 
document.write( strwrite ); 
</scrīpt> 
thisDLoc = document.location; <BR>
thisURL = document.URL; <BR>
thisHREF = document.location.href; <BR>
thisSLoc = self.location.href;<BR>
<scrīpt> 
thisTLoc = top.location.href; 
thisPLoc = parent.document.location; 
thisTHost = top.location.hostname; 
thisHost = location.hostname; 
strwrite = "<tr><td valign=top>thisTLoc: </td><td>[" + thisTLoc + "]</td></tr>" 
strwrite += "<tr><td valign=top>thisPLoc: </td><td>[" + thisPLoc + "]</td></tr>" 
strwrite += "<tr><td valign=top>thisTHost: </td><td>[" + thisTHost + "]</td></tr>" 
strwrite += "<tr><td valign=top>thisHost: </td><td>[" + thisHost + "]</td></tr>" 
document.write( strwrite ); 
</scrīpt> 
thisTLoc = top.location.href; <BR>
thisPLoc = parent.document.location; <BR>
thisTHost = top.location.hostname; <BR>
thisHost = location.hostname;<BR>
<scrīpt> 
tmpHPage = thisHREF.split( "/" ); 
thisHPage = tmpHPage[ tmpHPage.length-1 ]; 
tmpUPage = thisURL.split( "/" ); 
thisUPage = tmpUPage[ tmpUPage.length-1 ]; 
strwrite = "<tr><td valign=top>thisHPage: </td><td>[" + thisHPage + "]</td></tr>" 
strwrite += "<tr><td valign=top>thisUPage: </td><td>[" + thisUPage + "]</td></tr>" 
document.write( strwrite ); 
</scrīpt><tr><td>

 

转自:http://www.cnblogs.com/mbskys/articles/721527.html

在不同的编程语言中,获取当前网页的 URL 地址可以通过特定的内置变量或函数实现。以下是几种常见语言获取当前页面完整 URL 的方法。 ### PHP 中获取当前页面的完整 URL 可以通过 `$_SERVER` 数组来获取当前页面的完整 URL,包括协议、域名、端口和请求路径: ```php function getCurrentPageURL() { $pageURL = 'http'; if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') { $pageURL .= 's'; } $pageURL .= '://'; if ($_SERVER['SERVER_PORT'] != '80') { $pageURL .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI']; } else { $pageURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; } return $pageURL; } echo getCurrentPageURL(); ``` 如果只需要获取 URL 的路径部分(不包含查询参数),可以使用 `parse_url()` 函数进行解析: ```php function getCurrentPagePath() { return parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); } echo getCurrentPagePath(); ``` ### Python(Flask 框架)中获取当前页面的 URL 在 Flask 框架中,可以使用 `request.url` 来获取完整的 URL 地址: ```python from flask import Flask, request app = Flask(__name__) @app.route('/') def index(): return f'当前页面的完整 URL 是:{request.url}' if __name__ == '__main__': app.run() ``` 如果只需要获取路径部分,可以使用 `request.path`: ```python @app.route('/') def index(): return f'当前页面的路径是:{request.path}' ``` ### JavaScript 中获取当前页面的 URL 在前端 JavaScript 中,可以通过 `window.location` 对象获取当前页面的 URL 信息: ```javascript // 获取完整 URL const fullURL = window.location.href; console.log(fullURL); // 获取路径部分 const path = window.location.pathname; console.log(path); ``` ### Node.js(Express 框架)中获取当前页面的 URL 在 Express 框架中,可以通过 `req.protocol`、`req.get('host')` 和 `req.originalUrl` 来拼接完整的 URL: ```javascript app.get('/', (req, res) => { const fullURL = req.protocol + '://' + req.get('host') + req.originalUrl; res.send(`当前页面的完整 URL 是:${fullURL}`); }); ``` 如果只需要路径部分,可以直接使用 `req.path`: ```javascript app.get('/', (req, res) => { res.send(`当前页面的路径是:${req.path}`); }); ``` ### 总结 不同语言和框架中获取当前页面 URL 的方式有所不同,但核心逻辑是通过服务器或客户端的内置对象来提取协议、域名、端口和路径信息。PHP 中通过 `$_SERVER` 实现,Python 和 Node.js 框架中通过请求对象获取,而前端 JavaScript 则通过 `window.location` 对象实现[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值