最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Insert current URL into a link using JS and HTML - Stack Overflow

programmeradmin0浏览0评论

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=".html; title="This is a post!" target="_blank">

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>

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???

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?

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

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>

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???

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?

Share Improve this question asked Dec 7, 2015 at 23:37 DarksevenDarkseven 751 gold badge1 silver badge5 bronze badges
Add a comment  | 

7 Answers 7

Reset to default 3

This should do it:

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

Get the elements current href which doesn't have the url value and append the current url.

Modified HTML

<a id='smsharing' 
   href="http://reddit.com/submit?url="
   title="This is a post!"
   target="_blank">link</a>

Script

<script>
var x = window.location.href;
var link = document.getElementById("smsharing"); // store the element
var curHref = link.getAttribute('href'); // get its current href value
link.setAttribute('href', curHref + x);
</script>

Using just pure JavaScript you can set the href of the link by just having the base href as a string and then add the variable where ever it is needed.

var x = window.location.href;
document.getElementById("linkid").href = "http://reddit.com/submit?url="+encodeURIComponent(x);

Using jQuery it is as simple as:

$('a').attr("href",x);

You should simply replace innerHTML by href :

document.getElementById("smsharing").href = x;

Hope this helps.

Once you have access to the current URL, you then want to find the element and replace CURRENTPAGE.html. To do so, you'll need some way to select the element. Let's give it an ID:

<a id="myLink" href="http://reddit.com/submit?url=CURRENTPAGE.html"></a>

Now we can grab the link like so:

var link = document.getElement('myLink');

Let's get the URL again, and give it a better variable name:

var url = window.location.href;

Now let's update the HREF attribute of link:

link.href = link.href.replace('CURRENTPAGE.html', url);

And that's it!

Create another variable for the complete href attribute of your link:

var myURL = "http://reddit.com/submit?url=" + x;

Then replace the current href attribute with that variable:

docment.getElementById("YourLinkTagID").href = myURL

发布评论

评论列表(0)

  1. 暂无评论