How to replace links in proper href tags
Monney
I have six href
tabs body
. I selected all links and modified all of them. Now I want to use the modified link instead. My question is how to replace the link href
in the exact tag
var links = document.getElementsByTagName('a');
var new_str=[];
var new_utm=[];
var arr =[];
var utm_link_with_token = '&utm_source=iconnect&utm_medium=email&utm_campaign={{Approved_Document_vod__c.Vault_Document_ID_vod__c}}&emailId={{Approved_Document_vod__c.Vault_Document_ID_vod__c}}&codsId={{Account.CODS_External_Id__c}}&utm_term=';
var utm_link_without_token = '?utm_source=iconnect&utm_medium=email&utm_campaign={{Approved_Document_vod__c.Vault_Document_ID_vod__c}}&emailId={{Approved_Document_vod__c.Vault_Document_ID_vod__c}}&codsId={{Account.CODS_External_Id__c}}&utm_term='
for(var i = 0; i< links.length; i++){
arr.push(links[i].href);
}
console.log(arr);
for(var i = 0; i< arr.length; i++){
if ( (arr[i].indexOf("elqTrackId") > 0) ) {
if ( (arr[i].indexOf("?elqTrackId") > 0) ) {
new_str[i] = arr[i].substring(0, arr[i].indexOf("?elqTrackId"));
if ((new_str[i].indexOf("?ea=") > 0) ){
new_utm[i] = new_str[i].concat(utm_link_with_token);
}
else{
new_utm[i] = new_str[i].concat(utm_link_without_token);
}
}
else if ( (arr[i].indexOf("&elqTrackId") > 0) ) {
new_str[i] = arr[i].substring(0, arr[i].indexOf("&elqTrackId"));
if ((new_str[i].indexOf("?ea=") > 0) ){
new_utm[i] = new_str[i].concat(utm_link_with_token);
}
else{
new_utm[i] = new_str[i].concat(utm_link_without_token);
}
}
}
else if ( (arr[i].indexOf("mailto:") >= 0) || (arr[i].indexOf("tel:") >= 0) ) {
new_str[i] = arr[i];
new_utm[i] = new_str[i];
}
else{
new_str[i] = arr[i];
if ((new_str[i].indexOf("?ea=") > 0) ){
new_utm[i] = new_str[i].concat(utm_link_with_token);
}
else{
new_utm[i] = new_str[i].concat(utm_link_without_token);
}
}
}
console.log("link showed without eloqua tracking id", new_utm);
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<body width="100%" style="margin: 0; padding: 0 !important; mso-line-height-rule: exactly; background-color: #f4f4f4; width: 100% !important;">
<center style="width: 100% !important; background-color: #f4f4f4;">
<table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#f4f4f4" style="width:100% !important;">
<tbody><tr>
<td align="center" valign="top" class="">
<table class="container" cellpadding="0" cellspacing="0" border="0" width="600" bgcolor="#ffffff" style="margin: 0 auto;">
<!-- TEXT HEADER SECTION STARTS -->
<tbody>
<tr>
<td bgcolor="#ffffff" style="-moz-osx-font-smoothing: grayscale; -webkit-font-smoothing: antialiased; border-collapse: collapse; color: #717171; font-family: 'Verdana', 'Arial', 'Helvetica', sans-serif; font-size: 12px; font-smoothing: antialiased; font-weight: normal; line-height: 20px; margin: 0; padding: 0; padding: 10px; text-align: left; vertical-align: top; word-spacing: 2px; border: 2px solid #4089c3;">
<a href="https://www.google.com/dummy0?ea=8mYoHZtET8c&elqTrackId=a3a008f9213248fe8bb899351706a6ad">Lorem Ipsum</a> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the <a href="mailto:[email protected]">industry's</a> standard dummy text ever since the 1500s, when an unknown printer took a galley of type <a href="tel:012414 258">and</a> scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into <a href="http://www.google.com/dummy1?elqTrackId=2cdd5998b5024a23b2d21839786d24a8&elqTrack=true">electronic</a> typesetting, <a href="http://www.google.com/dummy2&elqTrackId=2cdd5998b5024a23b2d21839786d24a8&elqTrack=true">remaining</a> essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently <a href="http://www.google.com/dumm3">with</a> desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
</center>
</body></html>
Matt Ellen
I recommend replacing the text with a regular expression like this:
let links = document.getElementsByTagName('a');
for(let link of links)
{
let curhref = link.href;
if(curhref.indexOf('http') > -1)
{
link.href = curhref.replace(/(\?|&)elqTrackId=[a-z0-9]+(&elqTrack=true)?/, '');
if(link.href.indexOf('ea=') > -1)
{
link.href += '&utm_source=iconnect&utm_medium=email&utm_campaign={{Approved_Document_vod__c.Vault_Document_ID_vod__c}}&emailId={{Approved_Document_vod__c.Vault_Document_ID_vod__c}}&codsId={{Account.CODS_External_Id__c}}&utm_term=';
}
else
{
link.href += '?utm_source=iconnect&utm_medium=email&utm_campaign={{Approved_Document_vod__c.Vault_Document_ID_vod__c}}&emailId={{Approved_Document_vod__c.Vault_Document_ID_vod__c}}&codsId={{Account.CODS_External_Id__c}}&utm_term=';
}
}
}
<center style="width: 100% !important; background-color: #f4f4f4;">
<table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#f4f4f4" style="width:100% !important;">
<tbody><tr>
<td align="center" valign="top" class="">
<table class="container" cellpadding="0" cellspacing="0" border="0" width="600" bgcolor="#ffffff" style="margin: 0 auto;">
<!-- TEXT HEADER SECTION STARTS -->
<tbody>
<tr>
<td bgcolor="#ffffff" style="-moz-osx-font-smoothing: grayscale; -webkit-font-smoothing: antialiased; border-collapse: collapse; color: #717171; font-family: 'Verdana', 'Arial', 'Helvetica', sans-serif; font-size: 12px; font-smoothing: antialiased; font-weight: normal; line-height: 20px; margin: 0; padding: 0; padding: 10px; text-align: left; vertical-align: top; word-spacing: 2px; border: 2px solid #4089c3;">
<a href="https://www.google.com/dummy0?ea=8mYoHZtET8c&elqTrackId=a3a008f9213248fe8bb899351706a6ad">Lorem Ipsum</a> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the <a href="mailto:[email protected]">industry's</a> standard dummy text ever since the 1500s, when an unknown printer took a galley of type <a href="tel:012414 258">and</a> scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into <a href="http://www.google.com/dummy1?elqTrackId=2cdd5998b5024a23b2d21839786d24a8&elqTrack=true">electronic</a> typesetting, <a href="http://www.google.com/dummy2&elqTrackId=2cdd5998b5024a23b2d21839786d24a8&elqTrack=true">remaining</a> essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently <a href="http://www.google.com/dumm3">with</a> desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
</center>
Decompose the regular expression /(\?|&)elqTrackId=[a-z0-9]+(&elqTrack=true)?/
:
(\?|&)
: find?
or&
.?
is a special character, so it must be escaped to be used as is.elqTrackId=[a-z0-9]+
: FindelqTrackId=
followed by any number of lowercase letters a to z or 0 to 9(elqTrack=true)?
: also matcheselqTrack=true
if present . Parentheses create a capturing group, and?
the average matches 0 or 1 occurrence of that group.