简体   繁体   中英

How to capture / check all URL's with double slashes (//) and correct them (JQuery?)

At the moment we've got a big problem (temporarily). In our shop we're using a 3rd party solution for promoting and searching for products. We have around 3600 products which got harvested in the wrong way - the base-url had an extra forward slash ( www.sceneryworkshop.nl// ) so now all products are called the wrong way and end-up in nomansland. We started a new data-sync, but it takes a lot of time to get in sync.

So I'm looking for a temporarily solution, even if it's dirty. I don't have access to .htaccess or other server components. The shop runs on a closed-source platform (Lightspeed HQ) which is based on TWIG.

Anyone who knows how to change or redirect to the correct url? Javascript maybe? The sources are located and called from a lot of places, so the only solution would be a global function that checks and corrects the url's.

I really hope someone has an answer for me:-)

Hi i have a solution assuming that jquery is loaded after the dom is rendered

 $(document).ready(function() { $('a').each(function() { $split = $(this).attr('href').split('com//'); $(this).attr('href', $split[0] + 'com/' + $split[1]); console.log($(this).attr('href')); }) })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="https://www.google.com//search?q=Jquery" target="_blank">Jquery</a> <a href="https://www.google.com//search?q=Java" target="_blank">Java</a> <a href="https://www.google.com//search?q=J" target="_blank">J</a>
Note: If you like to test the link in the snipped; hold ctrl and click on the link.

Update Ok. i have read your comment and try some stuff in the inspector from your website. The following solution works!

 $('body a[href^="https://www.google.com//"]').each(function() { $x = $(this).attr('href').replace('com//', 'com/'); $(this).attr('href', $x) console.log($x); })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="https://www.google.com//search?q=Jquery" target="_blank">Jquery</a> <a href="https://www.google.com//search?q=Java" target="_blank">Java</a> <a href="https://www.google.com//search?q=J" target="_blank">J</a>

What i do here is simple, first i search every a element witch contains the following url: https://www.sceneryworkshop.nl// . then replace them with the correct url.

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