简体   繁体   中英

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.

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 $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">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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