Andi Andi - 2 months ago 23
Javascript Question

Javascript: Redirect to if URL contains value but keep that value

I have a subpage where I need to set a redirect to another subpage if the URL contains a specific value. However I need to keep that value and paste it to the redirect value.
Example:
Every URL with "/sub1/value=xy" should be redirected to "/sub2/value=xy"

mypage.com/sub1/value=1234 should be redirected to mypage.com/sub2/value=1234

I am checking the URL with the JS below, but I don't know how to get paste the value to the redirect URL



$(document).ready(function () {
if(window.location.href.indexOf("value=") > -1) {
}
});




Answer Source
$(document).ready(function () {
    if(window.location.href.indexOf("value=") > -1) {
        url=window.location.href;
        if(url.substr(url.indexOf('=')+1)=="your_specific_value")
            {
            url=url.replace("sub1","sub2");
            window.location.replace(url);
            }
    }
});

I think this is what is you want