![](/img/trans.png)
[英]anchor href get current url and append on next page with javascript
[英]Get current page URL in href
目前正在为 Vk 开发一个共享按钮,但是有一个动态站点,我需要该按钮自动使用正确的页面 URL。
这是他们为静态网站提供的代码片段,我该如何针对多个页面进行调整?
<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
。
所有网站上的简单作品
function share(){ function copyToClipboard(text) { window.prompt('share us',text); } copyToClipboard(window.location.href); }
<button onclick="javascript:share();">share </button>
使用 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.