javascript url编码

2019-03-25 17:28|来源: 网友

escape() 方法:

采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。
不会被此方法编码的字符: @ * / +


encodeURI() 方法:

把URI字符串采用UTF-8编码格式转化成escape格式的字符串。
不会被此方法编码的字符:! @ # $& * ( ) = : / ; ? + '

encodeURIComponent() 方法:

把URI字符串采用UTF-8编码格式转化成escape格式的字符串。与encodeURI()相比,这个方法将对更多的字符进行编码,比如 / 等字符。所以如果字符串里面包含了URI的几个部分的话,不能用这个方法来进行编码,否则 / 字符被编码之后URL将显示错误。
不会被此方法编码的字符:! * ( ) '
因此,对于中文字符串来说,如果不希望把字符串编码格式转化成UTF-8格式的(比如原页面和目标页面的charset是一致的时候),只需要使用escape。如果你的页面是GB2312或者其他的编码,而接受参数的页面是UTF-8编码的,就要采用encodeURI或者encodeURIComponent。
另外,encodeURI/encodeURIComponent是在javascript1.5之后引进的,escape则在javascript1.0版本就有。

本文链接:领悟书生教程网

相关问答

更多
  • 不需要这么麻烦啊。 如果你是 GB2312 编码,使用 escape 函数 如果你是 UTF8 编码 ,还可以使用 encodeURIComponent 函数 兼容 IE6 IE7 IE8 FF3.5
  • 期间不应该破坏网址,但我不知道你如何使用这段时间,所以我不能说真的。 我所知道的任何功能都没有编码''。 对于一个网址,这意味着你将不得不使用你自己的函数来编码'。'。 。 你可以base64对数据进行编码,但我不认为在js中有一种原生的方式。 您也可以用客户端和服务器端的ASCII等效(%2E)替换所有的句点。 基本上,通常不需要编码'。',所以如果你需要这样做,你需要提出自己的解决方案。 您可能还想进一步测试以确保'。' 会实际上破坏网址。 心连心 Periods shouldn't break the ...
  • 你可以做这样的事情: var myData = new Array('id=354313', 'fname=Henry', 'lname=Ford'); var url = myData.join('&'); You can do something like this: var myData = new Array('id=354313', 'fname=Henry', 'lname=Ford'); var url = myData.join('&');
  • 新新新200新200新200新200新200新200新200新200新200新200新200新200新200新200新200新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新 它不会分解URL的各个编码功能,您必须让它构建整个URL。 200新X-45旗新新200新200新200新200新200新新200新200新200新200新200新200新200新200 ...
  • 查看内置函数encodeURIComponent(str)和encodeURI(str) 。 在你的情况下,这应该是有效的: var myOtherUrl = "http://example.com/index.html?url=" + encodeURIComponent(myUrl); Check out the built-in function encodeURIComponent(str) and encodeURI(str). In your case, this should ...
  • 两个错误: 您需要访问位置对象的“href”成员: window.location的。 href = http://foo.com 您的JavaScript无效: javascript:location =' http : //validator.w3.org/check?uri='+ escape(location) PLUS SIGN AND QUOTE MISSING HERE &doctype = Inline&charset = detect + automatically&ss = 1&grou ...
  • 这是一个完全有效的网址: http://mysite.com/s/www.google.com 我怀疑你只是碰到了Rails format东西(例如,URL的末尾的.html将格式设置为HTML,JSON的JSON格式,...),所以你只需要修复你的路线,以保持格式自动检测阻碍: map.connect '/s/:url', :requirements => { :url => /.*/ }, ... 要么 match '/s/:url' => 'pancakes#house', :constraint ...
  • encodeURIComponent是正确的,但你只想在#ccc位上使用它。 如果您正在手动完成(如标记中的硬编码),则使用%23而不是# 。 encodeURIComponent is correct, but you only want to use it on the #ccc bit. If you are doing it by hand (as in, hardcoding in the tag), then use %23 instead of #.
  • 更正URI: 错误: http : //www.foo.com/foo.aspx? anotherURI?param1%3Daaa%26param2%3Dbbb 右: http : //www.foo.com/foo.aspx? anotherURI = param1%3Daaa%26param2%3Dbbb 多个URI的示例:http://www.foo.com/foo.aspc?uri1 = [encodedURI] &uri2 = [encodedURI2] 要从asp.net上的queryStrin ...
  • 您的选择是encodeURI和encodeURIComponent 。 encodeURIComponent是正确的选择,因为您正在编码URL的一部分(这恰好是类似于URL的,但这并不重要)。 如果您要使用encodeURI ,它将无法转换组件中足够的字符。 Your choices are encodeURI and encodeURIComponent. encodeURIComponent is the right choice because you are encoding part of the ...