-2

The following code, included in the header of a website, contains errors. I think the intention is clear by the code, but that it contains syntactical and/or other errors? Please correct.

Edit 2: full minimal example (js fiddle: https://jsfiddle.net/g1u2p36k/):

<html>

<head>
<script language="javascript">
function walkText(node) {
  if (node.nodeType == 3) {
    node.data = node.data.replace(/"Ashen Glow Gaming"/gi, "<span style=\"font-color: red;\">a<span style=\"color: blue\">shen</span> g<span style=\"color: blue\">low</span> g<span style=\"color: blue\">aming</span></span>");
  }
  if (node.nodeType == 1 && node.nodeName != "SCRIPT") {
    for (var i = 0; i < node.childNodes.length; i++) {
      walkText(node.childNodes[i]);
    }
  }
}
walkText(document.body);
</script>
</head>

<body>
blah blah Ashen Glow Gaming <a href="https://www.ashenglowgaming.com" class="Ashen Glow Gaming">an ashen glow gaming link</a> blah blah
</body>
</html>

This should result in all instances of the string "Ashen Glow Gaming" being replaced with a specially formatted version in the text:

enter image description here

Edit: JSLint identified 2 instances of Expected '\s' and instead saw ' '. and one instance of Expected '/'.:

enter image description here

3
  • What error do you get in console? Commented Jan 23, 2018 at 9:53
  • Jeez @Andy, it's not spam, don't downvote; I am working on that website and it happens to be the example I am using... Commented Jan 23, 2018 at 9:55
  • @gurvinder: Expected '\s' and instead saw ' '. and Expected '/'. Commented Jan 23, 2018 at 9:59

1 Answer 1

1

You are not escaping the quotes inside quotes.

node.data = node.data.replace(/"Ashen Glow Gaming"/gi, "<span style=\"font-family: elektora;\">a<span style=\"font-size: 80%\">shen</span> g<span style=\"font-size: 80%\">low</span> g<span style=\"font-size: 80%\">aming</span></span>");
Sign up to request clarification or add additional context in comments.

8 Comments

Please share a working snippet or fiddle demonstrating your issue.
I will give you as little info as possible but please fix my code Not sufficient to get the code working :)
@ptrcao Also mention what is not working. I can see that replace is happening just fine.
Apologies, the problem is I'm running this on a live website, I'm a non-coder, and I'm not using a console. How can I best go about conveying the information you need?
@gurvinder372 - in the JS fiddle, it doesn't implement the color changes for me?
|

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.