0

i use the code below to format some links. Where it can add either a suffix or a prefix to the link. But i have been researching how to remove part of the link.

Example, this link below.

https://www.torrid.com/product/boyfriend-straight-jean---vintage-stretch-medium-wash/14478822.html?cgid=Clothing_Jeans_Straight_Boyfriend#promo_id=210802_Jeans&promo_name=BoyfriendStraight_BoyfriendStraight&promo_creative=2107_FG_Denim_Boyfriend_Straight_277x702&promo_position=Jeans_Slide3&start=1

It has superfluous data, everything after

https://www.torrid.com/product/boyfriend-straight-jean---vintage-stretch-medium-wash/14478822.html

Isn't needed, how can i remove everything past that point when formatting the links, before adding the suffix or prefix. Thanks in advance for any help!

$("#btnGenerateLinks").on("click", function() {
 var valNeed = $("#strngtime").val();
 // if (valNeed.trim().length) { // For filter blank string
 $('input[name="linktype1"]').each(function() {
 $(this).val($(this).data("link") + valNeed);
 });
 $('input[name="linktype2"]').each(function() {
 $(this).val(valNeed + $(this).data("link"));
 });
 // }
});

Update - Yes all query parameters

Update - Going with a simple split for now

var myArr = valNeed.split("?")[0];
asked Sep 14, 2021 at 5:25
2
  • 1
    Remove part of a string or specifically remove the query parameters from a URL? Commented Sep 14, 2021 at 5:26
  • Yes all query parameters to make a clean link Commented Sep 14, 2021 at 5:27

1 Answer 1

2

you can use the URL constructor API

let url = "https://www.torrid.com/product/boyfriend-straight-jean---vintage-stretch-medium-wash/14478822.html?cgid=Clothing_Jeans_Straight_Boyfriend#promo_id=210802_Jeans&promo_name=BoyfriendStraight_BoyfriendStraight&promo_creative=2107_FG_Denim_Boyfriend_Straight_277x702&promo_position=Jeans_Slide3&start=1"
let instance = new URL(url);
let cleanURL = instance.origin + instance.pathname;
console.log(cleanURL);
// https://www.torrid.com/product/boyfriend-straight-jean---vintage-stretch-medium-wash/14478822.html
answered Sep 14, 2021 at 5:35
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.