0

Shortened up:

I need

<a href="http://itunes.com/Of Monsters and Men/My Head Is An Animal">View in iTunes</a>

to appear as

<a href="http://itunes.com/OfMonstersandMen/MyHeadIsAnAnimal">View in iTunes</a>

Detailed:

So basically what I'm doing is I'm trying to link to the itunes store I'll use this link as an example itunes.com/OfMonstersandMen/MyHeadIsAnAnimal.

This links directly to the item on the iTunes store. I'm using blogger and I'm going to automatically fill the links. The only problem with that is the links will appear as itunes.com/Of Monsters and Men/My Head Is An Animal. So what I need to do is use javascript or jquery to remove the spaces between the words in the link.

I've been looking all over for a solution. Is there anything I can do to fix this??

  • possible duplicate of [Replacing all occurrences of a string in JavaScript](http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript) – Matt Coubrough Jun 19 '14 at 05:22
  • 1
    What I don't understand is: if you replace the links, will the links still point to the right page? If not, where you want to '*preview*' the edited hrefs? – Roko C. Buljan Jun 19 '14 at 05:31

3 Answers3

2

jsBin demo

$('.view-itunes a[href*=" "]').prop('href', function(i, v){
  return v.replace(/%20/g,""); 
});

Cause the browsers translates blanks inside href with %20

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
  • Thanks you I just tried this and it worked, however I just realized the links are not going to appear with %20 instead they are appearing as itunes.com/Of Monsters and Men/My Head Is An Animal. I tried modifying the script to replace a space instead of %20 but it didn't work. Any ideas? – YoungSpiffy Jun 19 '14 at 05:38
  • @YoungSpiffy you clearly pointed in your question: "I want them to appear as: `http://itunes.com/OfMonstersandMen/MyHeadIsAnAnimal`" – Roko C. Buljan Jun 19 '14 at 05:42
  • I modified the script here http://jsbin.com/miyasofe/1/edit. I just need the spaces in the link inside the "view-itunes" div to be removed. Your script worked when removing %20 but the links on my website are appearing with a space instead of %20 – YoungSpiffy Jun 19 '14 at 05:47
  • Sorry I'm trying to explain it as best as I can, I want to remove the spaces in the href attribute property only. Also, I need the script to only remove the spaces on the link inside the "view-itunes" div so that any links outside that div are not affected – YoungSpiffy Jun 19 '14 at 05:54
  • And yes you are correct I want the link to appear as http://itunes.com/OfMonstersandMen/MyHeadIsAnAnimal instead of http://itunes.com/Of Monsters and Men/My Head Is An Animal – YoungSpiffy Jun 19 '14 at 05:55
  • @YoungSpiffy so actually as I can see from your demo you *don't* have `%20` but actually a blank space? – Roko C. Buljan Jun 19 '14 at 05:55
  • @YoungSpiffy Finally! you're welcome :) – Roko C. Buljan Jun 19 '14 at 06:07
0
<a href="http://itunes.com/Of%20Monsters%20and%20Men/My%20Head%20Is%20An%20Animal">View in iTunes</a>
<a id="result">View in iTunes</a>

<!--this is a example to result:-->
<b></b><br>

jquery

var aq = $('a').attr('href').split('%20').join('');
$('#result').attr('href',aq);

//this is a example to result:
$('b').text(aq);
alessandrio
  • 4,282
  • 2
  • 29
  • 40
-1

Just use string replace function

var a="test%20string";
var b=a.replace(/%20/g, "");
Mr.Cocococo
  • 1,401
  • 2
  • 11
  • 13