Home > Enterprise >  JS redirect function
JS redirect function

Time:12-16

I am trying to create a function that changes the redirect link of a button if the url contains the word "TicketPreDateable". So I have a button like this one:

<div  id="myproductid">
  <figure ><img  src="myimage">
    <figcaption><span  style="top: -2.7em; background-color: red;">OFFRE SPÉCIALE</span>
      <h2  style="color: white !important;"><span>My </span>Product Title</h2>
      <p>Profitez des pistes le samedi lorsque d'autres sont occupés à faire leurs valises ou coincés dans les bouchons. Un tarif unique à 24€ vous donne accès en toute liberté au domaine skiable pour la journée.</p>
      <a onclick="produrlmobile("https://www.mylink.com/fr/ProductsNgTicket/ticketPreDateable?poolNr=13&projNr=495&ticketTypeNr=122&preDatable=True&groupId=1&Day=11&Month=12&Year=2021")" href=""></a>
    </figcaption>
  </figure>
</div>

And my function look like this :

function produrlmobile(produrl) {
  if (produrl.includes("TicketPreDateable")) {
    if (window.innerWidth < 960) {
      window.location.href = produrl.replace(
        "TicketPreDateable",
        "TicketPreDateableMobile"
      );
    } else {
      window.location.href = produrl;
    }
  } else {
    window.location.href = produrl;
  }
}

I have the impression that it is because of special characters in the url that the function does not work, but I do not know how to avoid that.

Thank you for your help and have a nice day !

CodePudding user response:

See Sarens comment for issues with the HTML.

Also, note that the replace method is case sensitive. In your function it looks like you are trying to replace the string TicketPreDateable, but the url only contains ticketPreDateable. I would recommend checking you are handling cases correctly, for both the old and new strings in the replace method arguments.

  • Related