-1

I have a HTML element

<a class="toggle-tell-friend" id='share-button-1' rel="sidebar" data-id='share-content-1' data-title='<?php echo CHtml::decode($job->getTitle());?>' data-link="CURRENT URL">Friend</a>

How can I set the value of data-link to the current url.

abhit
  • 973
  • 3
  • 17
  • 40
  • 1
    Use this reference http://stackoverflow.com/questions/6768793/get-the-full-url-in-php – Sasikumar Sep 07 '16 at 08:15
  • [This](https://stackoverflow.com/questions/406192/get-current-url-in-javascript) is how you get url, and [this](https://stackoverflow.com/questions/13524107/how-to-set-data-attributes-in-html-elements) is how you set `data` attribute – vrs Sep 07 '16 at 08:20
  • This question isn't very clear. Not stating how exactly the user wants to implement this url. Through javascript? Maybe this includes an ajax call? I would assume there's more to it since googling "get current url javascript" isn't that hard. – NoobishPro Sep 07 '16 at 08:25

2 Answers2

2

Do you want it in javascript? Since you didn't put PHP in your tags, I would assume so.

In that case, you can use window.location.href to do it in javascript.

document.getElementById("share-button-1").setAttribute("data-link", window.location.href);

or in jquery:

$('#share-button-1').attr("data-link", window.location.href);

in case you did mean through PHP, you could use the following: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]. In this case I'd use it like this:

<?php
  $dataUrl = "http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
 ?>
<a class="toggle-tell-friend" id='share-button-1' rel="sidebar" data-id='share-content-1' data-title='<?php echo CHtml::decode($job->getTitle());?>' data-link="$dataUrl">Friend</a>
NoobishPro
  • 2,539
  • 1
  • 12
  • 23
1

You can do like this using location interface

var _getCurUrl = location.href;
$("#share-button-1").attr('data-link',_getCurUrl);

JSFIDDLE

brk
  • 48,835
  • 10
  • 56
  • 78