0

This is my javascript code below (Just to get current url)

<html>
<head>
</head>
<body onload="myFunction();">

<p id="myurl"></p>

<script>
function myFunction() {
    document.getElementById("myurl").innerHTML =
    window.location.host;
}
</script>
</body>
</html>

And Now i want to use <p id="myurl"></p> as below:

<a href="https://www.facebook.com/sharer/sharer.php?u=<p id="demo"></p>">Share on Facebook</a> <br>

But as it cant be execured inside href="value"
Is there any way to do that with php or something else?

6
  • in u= param you must pass only URL it will not accept any tag Commented Mar 16, 2015 at 7:12
  • is there any other method? Commented Mar 16, 2015 at 7:14
  • why you want to pass <p> tag?? Commented Mar 16, 2015 at 7:15
  • i want to get current url of page Commented Mar 16, 2015 at 7:16
  • What exactly you want? Why you place tag instead of value in get method? Please clear your need first.. Commented Mar 16, 2015 at 7:16

3 Answers 3

2

Use this :-

 <a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo $_SERVER['REQUEST_URI']; ?>">Share on Facebook</a> <br>
Sign up to request clarification or add additional context in comments.

Comments

0

With pure javascript (default url in href):

<a id="share_url" href="https://www.facebook.com/sharer/sharer.php?u=">Share on Facebook</a>

<script>
    function myFunction() {
        var el = document.getElementById("share_url");
        el.href += window.location.host;
    }
</script>

Demo: http://jsfiddle.net/Lrkjr23o/1/

Other way to create href attribute dynamically:

<a id="share_url">Share on Facebook</a>

<script>
    function myFunction() {
        var el = document.getElementById("share_url");
        el.href = 'https://www.facebook.com/sharer/sharer.php?u=';
        el.href += window.location.host;
    }
</script>

Demo: http://jsfiddle.net/Lrkjr23o/

Comments

0

I'm assuming client only solution is needed here, server can be anything (PHP, ASP, etc.) All you've to do is to modify the href property of anchor tag. You can modify your code as -

var elm = document.getElementById("demo");
var URLBase = elm.getAttribute("href");
var fullURL = URLBase.window.location.host;
elm.setAttribute("href", fullURL);

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.