7

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?

7 Answers 7

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>
Sign up to request clarification or add additional context in comments.

Comments

3

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>

Comments

2

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

1 Comment

Of course, you might want to append encodeURIComponent(x) instead of just x as a query string parameter.
1

Using jQuery it is as simple as:

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

Comments

0

You should simply replace innerHTML by href :

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

Hope this helps.

Comments

0

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!

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.