Javascript 以 HTML 格式显示网页的当前 URL(动态)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/3490658/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display current URL of webpage (dynamic) in HTML
提问by willium
I have this code in my HTML:
我的 HTML 中有此代码:
<h3 id="left">Lorem Ipsum </h3>
                <h3 id="right">[Current URL Here]</h3>
I want to display (dynamicly) the current URL inside the <h3>tags. I've been trying to figure it out for a few days, but I'm really a mobile developer, not an HTML developer, so it's proven difficult. I need this for an app I'm working on, so Please go easy on me :)
我想(动态)显示<h3>标签内的当前 URL 。几天来我一直试图弄清楚,但我确实是一名移动开发人员,而不是 HTML 开发人员,所以事实证明这很困难。我需要这个用于我正在开发的应用程序,所以请对我放轻松 :)
Thanks in advance.
提前致谢。
回答by MooGoo
document.getElementById('right').innerHTML = window.location.href;
回答by willium
If you wanted to do it in PHP, it's a little more involved:
如果你想用 PHP 来做,那就有点复杂了:
$url = !empty($_SERVER['HTTPS']) ? 'https://' : 'http://';
$url .= $_SERVER['HTTP_HOST'] . htmlspecialchars($_SERVER['REQUEST_URI']);
As aronasterlingpoints out, you need to sanitize $_SERVER['REQUEST_URI']to prevent XSS.
正如aronasterling指出的那样,您需要进行消毒$_SERVER['REQUEST_URI']以防止 XSS。
回答by The Mighty Rubber Duck
Well, you simply cannot do it in pure HTML.
好吧,您根本无法在纯 HTML 中做到这一点。
With javascript, you can go with
使用 javascript,您可以使用
<h3 id="right">
<script type="text/javascript">
document.write(location.href);
</script>
</h3>
Otherwise, if you are requesting a page on the server, you should rather have it done in there.
否则,如果您在服务器上请求一个页面,您应该在那里完成它。
回答by srinivas
the php code for getting complete url of current page is as follows
获取当前页面完整url的php代码如下
<?php 
    $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
    echo $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
?>
if you want to use javascript use the method suggested by @MooGoo
如果您想使用 javascript,请使用@MooGoo 建议的方法
full usage of that script is as follows
该脚本的完整用法如下
<SCRIPT LANGUAGE="JavaScript">        
   document.getElementById('right').innerHTML = window.location.href;       
</SCRIPT>
use this after you declared/defined <h3 id="right">[Current URL Here]</h3>
在声明/定义后使用它 <h3 id="right">[Current URL Here]</h3>
Hope helpful
希望有帮助
回答by makingbillions
<script type="text/javascript">
var segments = window.location.pathname.split('/');
var toDelete = [];
for (var i = 0; i < segments.length; i++) {
    if (segments[i].length < 1) {
        toDelete.push(i);
    }
}
for (var i = 0; i < toDelete.length; i++) {
    segments.splice(i, 1);
}
var filename = segments[segments.length - 1];
console.log(filename);
document.write(filename);
</script>
回答by vol7ron
While the JavaScript are what is more common, you could also use Server-Side Includes:
虽然 JavaScript 更常见,但您也可以使用服务器端包含:
<h3 id="right">
    <!--#echo var="SERVER_NAME" -->/<!--#echo var="DOCUMENT_URI" -->
</h3>
- instead of SERVER_NAMEyou can tryHTTP_HOST
- instead of DOCUMENT_URIyou can tryREQUEST_URI; one includes the query string, the other doesn't
- 而不是SERVER_NAME你可以尝试HTTP_HOST
- 而不是DOCUMENT_URI你可以尝试REQUEST_URI; 一个包含查询字符串,另一个不包含
回答by Murugesh
Php Code:
代码:
function curPageURL() {
 $pageURL = 'http';
 if ($_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;
}
<h3 id="right">echo curPageURL();</h3>

