0

I am producing links on my page using the $Html->link() menthod in cakePHP and my code is like this

echo $this->Html->link(substr($topsongs[$i]['song_details']['song_name'], 0, 18), array("?"=>array('song_name'=>$song,'song_id'=>$song_id)), array('class' => 'top_links link', 'id' => 'playlist-add' . $i, 'onclick' => "link_play(this.id);return false;"));

As seen in the code, I am passing the link id to the JS function. When retreving the song_name keystring , it is showing as an encoded string as it contains '+' instead of space and other characters. This song_name is actually a file name that is supplied to Flowplayer for playing. So it has to be free from the encoded things. How can I decode these variable in my Javascript function..?

2 Answers 2

1

you could either use some js decode: http://www.webtoolkit.info/javascript-url-decode-encode.html

or try to append the string for the file manually:

echo $this->Html->link($name, $this->Html->url(substr(...), true).'?song_name=foo');
Sign up to request clarification or add additional context in comments.

1 Comment

I have used this and it worked. Thanks a lot Mark. That saved my time.
0

I don't think there is a direct way in javascript to 'decode' that kind of string. What you can do is

link = link.replace('+', ' ');

and do the same for the other characters...

Ultimately, you can build a function, kind of like this:

function decode(string){
   var encoded = ['+', 'a', 'b', 'c'];
   var decoded = [' ', ' ', ' ', ' '];
   for (var i = 0; i < encoded.length; i++){
      string = string.replace(encoded[i], decoded[i]);
   }
   return string;
}

where you set up two arrays, first one with the characters you want to be replaced and the second one with the characters you want to replace them with, in the same order.

2 Comments

actually, it was quick method. But as it is using for playlist purpose, there is different URLS and different charecters. That is we don't actually know which all characters are going to be in the string.
you have to find some way to know what they are and map them in the arrays, there is no standard way to do it, to my knowledge

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.