简体   繁体   中英

Remove subdomain ONLY from url

How can I strip a form input URL of its subdomain and the subdomain only?

See JSfiddle or below code for example.

  $('[type="button"]').click(function() {
var btn = document.getElementById("download"); //get the button
var input = $('#soundcloud-url').val();

if (input.startsWith("https://soundcloud.com/") || input.startsWith("https://m.soundcloud.com/")) {
  btn.style.display = "inline-block"; //show the button
} else {
  btn.style.display = "none"; //hide the button
  alert('Enter a valid Souncloud url');
}
$('#soundcloud-iframe').attr('src', '//w.soundcloud.com/player/?' + $('form').serialize());
return false;

What I basically need is, if someone enters an url in the form from the mobile Soundcloud website, http://m.souncloud ..., the URL converts to the normal URL automatically: without the ".m" part... But if someone enters the normal URL (non-mobile), nothing should happen.

Should be an easy fix but I cant figure it out..

You can strip out the m. subdomain with a JS replace like this:

 $('#submit').on('click', function() { var input = $('#soundcloud-url').val().replace('//m.', '//'); console.log('stripped input:', input); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> URL: <input id="soundcloud-url" value="https://m.soundcloud.com/neilcic/no-credit-card" /> <button id="submit">GO</button> 

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