0

I'm currently working on a cms that will have an alert appear on multiple pages. I am currently using an if statement to have the alert appear only a page with a specific page title. Is there a way of generalizing it and having it appear on all articles with the word "Test" in the title?

At the moment my logic is if @pageTitle === "Test Article Two display....

I tried doing @pageTitle === "Test" but that only shows on the article that has the title Test rather then other titles with the word Test in them.

Here is my code :

<script>
    if(document.title === "Test Article Two") {
        document.body.classList.add("show-alert");
    }
</script>
neoslo
  • 353
  • 1
  • 3
  • 19

2 Answers2

1

You can use the JavaScript string method includes: if (document.title.includes('Test')).

C G
  • 81
  • 5
1

Methods -

Regex, case sensitive:

if (/Test/.test(document.title)) { ... }

Regex, case insensitive

if (/test/i.test(document.title)) { ... }

indexOf, case sensitive, (fastest)

if (document.title.indexOf("Test") !== -1) { ... }

includes (ES6), case sensitive

if (document.title.includes("Test")) { ... }
Rubydesic
  • 3,386
  • 12
  • 27