Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JS redirect function

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 class="col-12 col-md-6 col-xl-4" id="myproductid">
                <figure class="effect-product mx-auto"><img class="figure-img img-fluid" src="myimage">
                    <figcaption><span class="badge badge-danger" style="top: -2.7em; background-color: red;">OFFRE SPÉCIALE</span>
                        <h2 class="mt-0" 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you for your help and have a nice day !

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading