Replace a keyword in a web page with a hyperlink avoiding a and h tags












0















I have the following script that will replace keywords within a website with a hyperlink, but it's screwing up hyperlinks that already contain the keyword. How can I avoid replacing keywords that are within a and h tags?



<!-- include JQuery from Google CDN -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// https://findandreplace.bubbleapps.io/version-test/1.1/obj/keywords

// set Async false for .each loop to work synchronously
$.ajaxSetup({ async: false });

txt = $('body').html();
//alert (txt);

$.ajax({
url: "https://findandreplace.bubbleapps.io/version-test/api/1.1/obj/keywords",
success: function(data){
//console.log(data);
$.each(data.response.results, function() {
keyword = this['keyword'];
url = this['url'];

var regex = new RegExp(keyword, "ig");

replaceURL = "<a href='" + url + "'>" + keyword + "</a>";
txt = txt.replace(regex, replaceURL);
}); //end loop through keywords
//alert("after keyword " + keyword + "----" + txt)
} //end success function
}); //end ajax call

$('body').html(txt);

//reset JS sync to true
$.ajaxSetup({async: true});
}); // end doc ready

</script>


You can see an example here: https://slybaldguys.com/keywordtest2.html










share|improve this question


















  • 1





    Don't use regex on html

    – charlietfl
    Nov 14 '18 at 19:23











  • Also never ever use async:false for ajax. It is a horrible practice and browser vendors have deprecated it. You should be seeing a warning about that in browser dev tools console

    – charlietfl
    Nov 14 '18 at 19:26













  • @charlietfl any suggestions on what I should do to correct this?

    – Tyler S
    Nov 14 '18 at 19:31











  • What you will really need to do is iterate through text nodes in the document and only target them. A search for terms like "javascript or jquery replace text" should get you started

    – charlietfl
    Nov 14 '18 at 19:34











  • @charlietfl I've searched that and other variations hundreds of times and haven't found the answer I'm looking for, hence why I posted my question.

    – Tyler S
    Nov 14 '18 at 19:43
















0















I have the following script that will replace keywords within a website with a hyperlink, but it's screwing up hyperlinks that already contain the keyword. How can I avoid replacing keywords that are within a and h tags?



<!-- include JQuery from Google CDN -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// https://findandreplace.bubbleapps.io/version-test/1.1/obj/keywords

// set Async false for .each loop to work synchronously
$.ajaxSetup({ async: false });

txt = $('body').html();
//alert (txt);

$.ajax({
url: "https://findandreplace.bubbleapps.io/version-test/api/1.1/obj/keywords",
success: function(data){
//console.log(data);
$.each(data.response.results, function() {
keyword = this['keyword'];
url = this['url'];

var regex = new RegExp(keyword, "ig");

replaceURL = "<a href='" + url + "'>" + keyword + "</a>";
txt = txt.replace(regex, replaceURL);
}); //end loop through keywords
//alert("after keyword " + keyword + "----" + txt)
} //end success function
}); //end ajax call

$('body').html(txt);

//reset JS sync to true
$.ajaxSetup({async: true});
}); // end doc ready

</script>


You can see an example here: https://slybaldguys.com/keywordtest2.html










share|improve this question


















  • 1





    Don't use regex on html

    – charlietfl
    Nov 14 '18 at 19:23











  • Also never ever use async:false for ajax. It is a horrible practice and browser vendors have deprecated it. You should be seeing a warning about that in browser dev tools console

    – charlietfl
    Nov 14 '18 at 19:26













  • @charlietfl any suggestions on what I should do to correct this?

    – Tyler S
    Nov 14 '18 at 19:31











  • What you will really need to do is iterate through text nodes in the document and only target them. A search for terms like "javascript or jquery replace text" should get you started

    – charlietfl
    Nov 14 '18 at 19:34











  • @charlietfl I've searched that and other variations hundreds of times and haven't found the answer I'm looking for, hence why I posted my question.

    – Tyler S
    Nov 14 '18 at 19:43














0












0








0








I have the following script that will replace keywords within a website with a hyperlink, but it's screwing up hyperlinks that already contain the keyword. How can I avoid replacing keywords that are within a and h tags?



<!-- include JQuery from Google CDN -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// https://findandreplace.bubbleapps.io/version-test/1.1/obj/keywords

// set Async false for .each loop to work synchronously
$.ajaxSetup({ async: false });

txt = $('body').html();
//alert (txt);

$.ajax({
url: "https://findandreplace.bubbleapps.io/version-test/api/1.1/obj/keywords",
success: function(data){
//console.log(data);
$.each(data.response.results, function() {
keyword = this['keyword'];
url = this['url'];

var regex = new RegExp(keyword, "ig");

replaceURL = "<a href='" + url + "'>" + keyword + "</a>";
txt = txt.replace(regex, replaceURL);
}); //end loop through keywords
//alert("after keyword " + keyword + "----" + txt)
} //end success function
}); //end ajax call

$('body').html(txt);

//reset JS sync to true
$.ajaxSetup({async: true});
}); // end doc ready

</script>


You can see an example here: https://slybaldguys.com/keywordtest2.html










share|improve this question














I have the following script that will replace keywords within a website with a hyperlink, but it's screwing up hyperlinks that already contain the keyword. How can I avoid replacing keywords that are within a and h tags?



<!-- include JQuery from Google CDN -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// https://findandreplace.bubbleapps.io/version-test/1.1/obj/keywords

// set Async false for .each loop to work synchronously
$.ajaxSetup({ async: false });

txt = $('body').html();
//alert (txt);

$.ajax({
url: "https://findandreplace.bubbleapps.io/version-test/api/1.1/obj/keywords",
success: function(data){
//console.log(data);
$.each(data.response.results, function() {
keyword = this['keyword'];
url = this['url'];

var regex = new RegExp(keyword, "ig");

replaceURL = "<a href='" + url + "'>" + keyword + "</a>";
txt = txt.replace(regex, replaceURL);
}); //end loop through keywords
//alert("after keyword " + keyword + "----" + txt)
} //end success function
}); //end ajax call

$('body').html(txt);

//reset JS sync to true
$.ajaxSetup({async: true});
}); // end doc ready

</script>


You can see an example here: https://slybaldguys.com/keywordtest2.html







javascript jquery ajax






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 19:19









Tyler STyler S

11




11








  • 1





    Don't use regex on html

    – charlietfl
    Nov 14 '18 at 19:23











  • Also never ever use async:false for ajax. It is a horrible practice and browser vendors have deprecated it. You should be seeing a warning about that in browser dev tools console

    – charlietfl
    Nov 14 '18 at 19:26













  • @charlietfl any suggestions on what I should do to correct this?

    – Tyler S
    Nov 14 '18 at 19:31











  • What you will really need to do is iterate through text nodes in the document and only target them. A search for terms like "javascript or jquery replace text" should get you started

    – charlietfl
    Nov 14 '18 at 19:34











  • @charlietfl I've searched that and other variations hundreds of times and haven't found the answer I'm looking for, hence why I posted my question.

    – Tyler S
    Nov 14 '18 at 19:43














  • 1





    Don't use regex on html

    – charlietfl
    Nov 14 '18 at 19:23











  • Also never ever use async:false for ajax. It is a horrible practice and browser vendors have deprecated it. You should be seeing a warning about that in browser dev tools console

    – charlietfl
    Nov 14 '18 at 19:26













  • @charlietfl any suggestions on what I should do to correct this?

    – Tyler S
    Nov 14 '18 at 19:31











  • What you will really need to do is iterate through text nodes in the document and only target them. A search for terms like "javascript or jquery replace text" should get you started

    – charlietfl
    Nov 14 '18 at 19:34











  • @charlietfl I've searched that and other variations hundreds of times and haven't found the answer I'm looking for, hence why I posted my question.

    – Tyler S
    Nov 14 '18 at 19:43








1




1





Don't use regex on html

– charlietfl
Nov 14 '18 at 19:23





Don't use regex on html

– charlietfl
Nov 14 '18 at 19:23













Also never ever use async:false for ajax. It is a horrible practice and browser vendors have deprecated it. You should be seeing a warning about that in browser dev tools console

– charlietfl
Nov 14 '18 at 19:26







Also never ever use async:false for ajax. It is a horrible practice and browser vendors have deprecated it. You should be seeing a warning about that in browser dev tools console

– charlietfl
Nov 14 '18 at 19:26















@charlietfl any suggestions on what I should do to correct this?

– Tyler S
Nov 14 '18 at 19:31





@charlietfl any suggestions on what I should do to correct this?

– Tyler S
Nov 14 '18 at 19:31













What you will really need to do is iterate through text nodes in the document and only target them. A search for terms like "javascript or jquery replace text" should get you started

– charlietfl
Nov 14 '18 at 19:34





What you will really need to do is iterate through text nodes in the document and only target them. A search for terms like "javascript or jquery replace text" should get you started

– charlietfl
Nov 14 '18 at 19:34













@charlietfl I've searched that and other variations hundreds of times and haven't found the answer I'm looking for, hence why I posted my question.

– Tyler S
Nov 14 '18 at 19:43





@charlietfl I've searched that and other variations hundreds of times and haven't found the answer I'm looking for, hence why I posted my question.

– Tyler S
Nov 14 '18 at 19:43












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307367%2freplace-a-keyword-in-a-web-page-with-a-hyperlink-avoiding-a-and-h-tags%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307367%2freplace-a-keyword-in-a-web-page-with-a-hyperlink-avoiding-a-and-h-tags%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python