Carl Schoenleber
Carl Schoenleber

Reputation: 29

Change an <h1> based on URL with JS

I am setting up a test in Optimizely that will run on a number of pages. The pages are identical with the exception of the headline, but unfortunately if you change any HTML it ends up hard coding the headline into the variation design.

I'm just really learning JS and so far I've butchered the script.

$(document).ready(function () {
if(window.location.href.indexOf("calculate-metal-roofing-costs") > -1)
    $("h1").text("Request Your Metal Roofing Cost Estimates");
}  else {
if(window.location.href.indexOf("calculate-shingle-squares") > -1)
    $("h1").text("Request Your Shingle Estimates");
});

Can anyone help me with this, even just aim this unfortunate soul in the right direction? Any responses will be greatly appreciated.

Upvotes: 1

Views: 608

Answers (1)

Lee
Lee

Reputation: 3101

$(document).ready(function () {
  if (window.location.href.indexOf("calculate-metal-roofing-costs") > -1) {
    $("h1").text("Request Your Metal Roofing Cost Estimates");
  }
  if (window.location.href.indexOf("calculate-shingle-squares") > -1) {
    $("h1").text("Request Your Shingle Estimates");
  }
});

Upvotes: 1

Related Questions