Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert current URL into a link using JS and HTML

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?

like image 672
Darkseven Avatar asked Dec 07 '15 23:12

Darkseven


People also ask

How do I get the current URL in HTML?

Answer: Use the window. location. href Property location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc. The following example will display the current url of the page on click of the button.


1 Answers

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>
like image 104
Sharanya Avatar answered Oct 19 '22 23:10

Sharanya