-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

ptrcao
  • 421
  • 1
  • 5
  • 19

1 Answers1

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>");
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • Please share a working snippet or fiddle demonstrating your issue. – gurvinder372 Jan 23 '18 at 10:03
  • 1
    I will give you as little info as possible but please fix my code `Not sufficient to get the code working` :) – Nope Jan 23 '18 at 10:05
  • @ptrcao Also mention what is not working. I can see that replace is happening just fine. – gurvinder372 Jan 23 '18 at 10:16
  • 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? – ptrcao Jan 23 '18 at 10:18
  • @gurvinder372 - in the [JS fiddle](https://jsfiddle.net/g1u2p36k/), it doesn't implement the color changes for me? – ptrcao Jan 23 '18 at 10:22
  • @gurvinder372: Could this have to do with `if (node.nodeType == 1 && node.nodeName != "SCRIPT")` - I'm not sure about the significance of "SCRIPT" as this is copy-and-past by a JS-illiterate. – ptrcao Jan 23 '18 at 10:24
  • @ptrcao That part is fine.. Problem is that script tag should come after body, else it won't have access to the DOM which hasn't yet loaded on browser. – gurvinder372 Jan 23 '18 at 11:35
  • @gurvinder372 Ah okay - I got it to work - also in part because the quotes around "Ashen Glow Gaming" should not have been there. – ptrcao Jan 23 '18 at 15:07