简体   繁体   English

在 href 中获取当前页面 URL

[英]Get current page URL in href

Currently working on a share button for Vk however having a dynamic site, I need the button to automatically use the correct page URL.目前正在为 Vk 开发一个共享按钮,但是有一个动态站点,我需要该按钮自动使用正确的页面 URL。

This is the code snippet they offer for a static site, how could I adjust this for multiple pages ?这是他们为静态网站提供的代码片段,我该如何针对多个页面进行调整?

<a href="http://vk.com/share.php?url=http://example.com" target="_blank">

最简单的方法是通过 JavaScript:

 document.getElementById("vkShare").href = 'http://vk.com/share.php?url='+document.location.href;
 <a id="vkShare" target="_blank">Share to VK</a>

您可以使用像这样的窗口对象来获取当前页面的 URL: window.location.href

simple works on all sites所有网站上的简单作品

 function share(){ function copyToClipboard(text) { window.prompt('share us',text); } copyToClipboard(window.location.href); }
 <button onclick="javascript:share();">share </button>

Using php to get the url, try to use $_SERVER['HTTP_HOST'] to get the host and $_SERVER['REQUEST_URI'] to get any query string attached to the url.使用 php 获取 url,尝试使用$_SERVER['HTTP_HOST']获取主机和$_SERVER['REQUEST_URI']获取附加到 url 的任何查询字符串。

 <?php $uri = $_SERVER['REQUEST_URI']; $host=$_SERVER['HTTP_HOST']; //$complete_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; //To use whether HTTP OR HTTPS if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === "on"){ $htp = "https"; }else{ $htp = "http"; } $complete_url = $htp."//:".$host.$uri; ?> <a href="<?=$complete_url?>" target="_blank">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM