简体   繁体   English

用JavaScript解码CakePHP编码的URL

[英]Decoding CakePHP encoded URLs in Javascript

I am producing links on my page using the $Html->link() menthod in cakePHP and my code is like this 我正在使用cakePHP中的$ Html-> link()方法在页面上生成链接,而我的代码是这样的

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. 如代码所示,我将链接ID传递给JS函数。 When retreving the song_name keystring , it is showing as an encoded string as it contains '+' instead of space and other characters. 撤回song_name字符串时,它显示为编码字符串,因为它包含'+'而不是空格和其他字符。 This song_name is actually a file name that is supplied to Flowplayer for playing. 此song_name实际上是提供给Flowplayer进行播放的文件名。 So it has to be free from the encoded things. 因此,它必须没有编码的东西。 How can I decode these variable in my Javascript function..? 如何在我的Javascript函数中解码这些变量。

you could either use some js decode: http://www.webtoolkit.info/javascript-url-decode-encode.html 您可以使用一些js解码器: 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');

I don't think there is a direct way in javascript to 'decode' that kind of string. 我认为javascript中没有直接方法“解码”这种字符串。 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. 在这里,您设置了两个数组,第一个数组具有要替换的字符,第二个数组具有要替换的字符,顺序相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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