2

I need to add event onClick="domainname" to all links available in a text message without change nothing in these links. i have an idea to use str_replace() but i don't have much knowledge in pattern matching. i need a help for this matter.

1 Answers1

0

As far as I understand I am giving you two answers due to cofusion about what you really want first: Say You have links like these in your message

 <a href="www.xyz.com">xyz</a>

to change these to some other link without removing the href attribute use code like this

<script>
    $('.a').on('click',function(e){
        e.preventDefault();
        location.href = 'abc.com';
    });
</script>

this take you to abc.com even though the href attribute states xyz.com

Now second case is If you want to directly change the xyz.com of the tag to abc.com use something like this

<script>
        $('.a').attr('href','www.yahoo.com');
    </script>

this will change all the tags attributes to xyz.com so all the links in that page will point to abc.com

xenish
  • 1,384
  • 3
  • 14
  • 17
  • Hi, Thanks for your reply, the purpose to add onClick event on each links available in a text message is to track if a user click the link in his inbox. i can use jquery/javascript or php to do this task but i have no idea. user will post a text message, in this text it may have a link or not. if link available i have to add the onclick event to each links in the posted message. i hop now you get the idea. – chamssoudine bacar May 03 '15 at 07:27
  • you want to add onclick events only if the links are present @chamssoudinebacar – xenish May 03 '15 at 09:08
  • Yes and the onClick should point to an external server like : onClick="http://somedomain.com/tracking.php?user_id=1&msg=99". – chamssoudine bacar May 03 '15 at 09:12
  • so as far as I understand you want all the links in the message to point to the above link (http://somedomain.com/?f) , so use the above code what the above code does is it selects all the anchor tag which are the links and when user clicks on the link the function runs, inside the function use this code window.location.hash = 'your link'; return false; this will send the user to this link – xenish May 03 '15 at 09:17
  • Let a message text be like: text = "I need to add onclick event to link available in this message. when user click on this link Click here. if you need to subscribe click here : Click here" final output i need to look like this .text = "I need to add onclick event to all link available in this message. when user click on this link Click here. if you need to subscribe click here : Click here" – chamssoudine bacar May 03 '15 at 09:28
  • so you want to change all the href attributes of links in a message to mydomain.com?tracking.php?id= for example: Click here this to Click here – xenish May 03 '15 at 09:30
  • was it useful @chamssoudinebacar – xenish May 03 '15 at 09:41
  • href attributes of links should be there also and remain unchanged. – chamssoudine bacar May 03 '15 at 09:54
  • i have edited the answer check it out and see if it helps @chamssoudinebacar – xenish May 03 '15 at 10:35
  • Thanks for your help but you did not get well what i need. this message is sent to inbox like yahoo or google mail. i need to know if the receiver click on any link included in the message body. so my concern is to add the onclick event to send a request to my domain. because the links inside has already the href point to other domain and should be remain unchanged as this is a link for the sender.think for email campaign or newsletter. – chamssoudine bacar May 03 '15 at 11:35