简体   繁体   中英

How can I create a dynamic link changing just part of the URL?

I'm working on a asp.net e-commerce site and we have launched a new version of the site on a subdomain.

We want to offer customers the ability to gradually test the Beta site.

I'd like to add a link to the product template page for each product where the customer goes directly to the same page on the new site. It's not practical to do this manually because we have hundreds of products.

So if the customer is viewing http://www.example.com/category/product/colour/ by clicking on the link (which will be a banner image) they go to: http://new.example.com/category/product/colour/

I'm assuming you can do this in javascript, where it takes an element of the URL and appends it to specific domain but my JS is very rusty. Can anyone help with this one please?

Here is a sample code:

var url = document.URL;
var res = url.replace("example.com", "new.example.com"); 
document.location = res;

Also, you can set this code with a click event listener to the banner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM