使用JS和HTML将当前URL插入链接 [英] Insert current URL into a link using JS and HTML

查看:99
本文介绍了使用JS和HTML将当前URL插入链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经阅读了类似的内容,但仍然找不到与我的工作更为接近的答案.我正在尝试使用JS来获取当前页面URL,并将其附加到这样的社交媒体共享链接上:

So, Ive read through similar things but I still can't find an answer that applies more closely to what I'm doing. I am attempting to use JS to get the current page URL and append it to a social media sharing link like this:

<a href="http://reddit.com/submit?url=CURRENTPAGE.html; title="This is a post!" target="_blank">

使用Javascript,我设法将当前URL分配给变量:

Using Javascript, I've managed to assign the current URL to a variable:

<script>
var x = window.location.href;
document.getElementById("smsharing").innerHTML = x;
</script></p>

我通过对其进行测试显示来确保它可以正常工作.那么实际上将'x'代替CURRENTPAGE.html的正确方法/语法到底是什么?

And I made sure it worked by doing a test display of it. So what exactly is the proper method/syntax for actually putting 'x' in place of CURRENTPAGE.html???

我知道这是一个愚蠢的问题,但我真的很沮丧.细节会有所帮助,因为部分问题是我对JS很少了解.有想法吗?

I know this is a STUPID question, but I'm really stumped. Specifics help, because part of the problem is that I have precious little knowledge of JS. Thoughts?

推荐答案

这应该做到:

<script>
baseurl="http://www.facebook.com?"
function buildURL(item)
{
    item.href=baseurl+window.location.href;
    return true;
}
</script>
</head>
<body>
<a onclick="return buildURL(this)" href="">Google</a>
</body>

这篇关于使用JS和HTML将当前URL插入链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆