ste*_*ail 3 javascript php jquery uri urlencode
每当点击链接时,我都会通过URL将字符串传递给另一个页面.
原始a标记及其href是通过PHP while循环创建的,如下所示:
<a href=".($row['secondary'] == 'true' ? "secondary_imgs.php?imgId=".$row['imgId']."
&imgTitle=".$row['imgTitle']."" : "new_arrivals_img/".$row['imgURL']."").">
Run Code Online (Sandbox Code Playgroud)
注:该a标签不包含其他属性,但我删除他们简单.
将href然后被附加,在当前窗口的尺寸,使用下列jQuery函数:
$("a[href^='secondary_imgs.php']").click(function(){
var pageWidth = $(window).width();
var pageHeight = $(window).height();
var url = $(this).attr('href');
var idx = url.indexOf('&pageWidth=');
if (idx != -1) {
url = url.substr(0, idx);
}
url += '&pageWidth=' + pageWidth + '&pageHeight=' + pageHeight;
$(this).attr('href', url);
});
Run Code Online (Sandbox Code Playgroud)
我的问题是,在这种情况下我应该使用哪种编码URL的方法,因为它需要首先使用PHP,然后是jQuery/JavaScript,最后通过PHP解码?或者将它必须同时在PHP和jQuery/JavaScript来做些什么呢?原来PHP $row['imgTitle']将始终至少有空间在里面,所以我知道它需要,于是做了肯定.但是看到附加值总是一个等于数字的变量,那还需要编码吗?
我知道在每种编码语言中有几种编码/解码URL的方法,而我的知识还不足以做出选择.
如果有人也可以举例说明我将如何执行每一步,那将非常感激.
您应该urlencode()在PHP中使用来编码URI组件.
javascript中的等效函数是encodeURIComponent().
PHP将自动解码这样编码的参数.
那么,在你的PHP中:
'<a href="..&imgTitle='.urlencode($row['imgTitle'])
Run Code Online (Sandbox Code Playgroud)
等效地,在你的javascript中:
'&someparam=' + encodeURIComponent(someValue)
Run Code Online (Sandbox Code Playgroud)
为什么需要先用PHP完成?这个JS urlencode函数可以从PHP进行urldecoded.编码一次,解码一次:
function urlencode (str) {
// URL-encodes string
//
// version: 1107.2516
// discuss at: http://phpjs.org/functions/urlencode
// + original by: Philip Peterson
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: AJ
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: travc
// + input by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Lars Fischer
// + input by: Ratheous
// + reimplemented by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Joris
// + reimplemented by: Brett Zamir (http://brett-zamir.me)
// % note 1: This reflects PHP 5.3/6.0+ behavior
// % note 2: Please be aware that this function expects to encode into UTF-8 encoded strings, as found on
// % note 2: pages served as UTF-8
// * example 1: urlencode('Kevin van Zonneveld!');
// * returns 1: 'Kevin+van+Zonneveld%21'
// * example 2: urlencode('http://kevin.vanzonneveld.net/');
// * returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
// * example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
// * returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
str = (str + '').toString();
// Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
// PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}
Run Code Online (Sandbox Code Playgroud)