网址解析 [英] url parsing

查看:88
本文介绍了网址解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下内容来解析网址


var srch = window.location.search.substring(1);

//然后拆分srch在&符号:

var parts = srch.split("&");

//将参数写入变量

for(var i in parts){

var temp = parts [i] .split(" =");

if(temp [0] == xx){xx = 1 * temp [1]; }

if(temp [0] ==" yy"){yy = 1 * temp [1]; }

if(temp [0] ==" ff"){ff = 1 * temp [1]; }

if(temp [0] ==" level"){level = 1 * temp [1]; }

}


然而,当我解析网址时,它会在带有&符号的情况下呈现

时一直窒息。我是否需要以某种方式逃避这个角色?

-

-

Fabian

经常访问我的网站并且长期使用!
http://www.lajzar.co.uk

解决方案

Fabian hu kiteb:

我使用以下内容来解析url

var srch = window.location.search.substring(1);
//然后在&符号上拆分srch:
var parts = srch.split(&& ;");




这条线肯定是最重要的。当我使用a时而不是&,它

正确解析多个子串。


var parts = srch.split(" a");


不幸的是,这不是将数据包含在

url中的标准方法,所以虽然它有效,但它不是最佳的。


-

-

Fabian

经常长时间访问我的网站!
http://www.lajzar.co.uk


"费边" < LA **** @ hotmail.com>写道:

我使用以下内容来解析网址

var srch = window.location.search.substring(1);
//然后在&符号上拆分srch:
var parts = srch.split("&");
....但是,当我解析网址时,它会在带有&符号的情况下一直窒息。我是否需要以某种方式逃避这个角色?




这取决于。


如果代码嵌入在HTML页面中,那么&符号应该是
转义(作为HTML实体,& amp;",而不是Javascript字符串转义

" \&" )。如果它在外部javascript文件中,那么它不应该。


所以,可能。你试过逃避它吗?


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡颜色:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> <没有判断力的信仰只会降低神灵的精神。''


Lasse Reichstein Nielsen hu kiteb:

但是,当我解析网址时,它会在带有&符号的情况下一直窒息。我是否需要以某种方式逃避这个角色?



这取决于。

如果代码嵌入在HTML页面中,那么&符应该被转义(作为HTML实体,& amp;,而不是Javascript字符串转义
" \&")。如果它在外部javascript文件中,那么它不应该。

所以,可能。你试过逃避吗?




它在一个外部的javascript文件中。我试过逃避它,并且没有一个我认识的逃脱序列似乎有很大的不同。我试过了

%26 ,,& amp;和\ u0026。我不知道

逃避角色的其他方法。

-

-

Fabian

经常长时间访问我的网站!
http://www.lajzar.co.uk


I use the following to parse the url

var srch = window.location.search.substring(1);
// then split srch at the ampersand:
var parts = srch.split("&");
// write the parameters into the variables
for(var i in parts) {
var temp = parts[i].split("=");
if (temp[0] == "xx") { xx = 1 * temp[1]; }
if (temp[0] == "yy") { yy = 1 * temp[1]; }
if (temp[0] == "ff") { ff = 1 * temp[1]; }
if (temp[0] == "level") { level = 1 * temp[1]; }
}

However, when I parse the url, it consistently chokes when presented
with an ampersand character. Do I need to escape this character somehow?
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

解决方案

Fabian hu kiteb:

I use the following to parse the url

var srch = window.location.search.substring(1);
// then split srch at the ampersand:
var parts = srch.split("&");



This line is definately the big isue. When I use "a" instead of "&", it
parses multiple substrings correctly.

var parts = srch.split("a");

Unfortunately, this isn''t the standard approach to including data in an
url, so while it works, it is sub-optimal.

--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk


"Fabian" <la****@hotmail.com> writes:

I use the following to parse the url

var srch = window.location.search.substring(1);
// then split srch at the ampersand:
var parts = srch.split("&"); .... However, when I parse the url, it consistently chokes when presented
with an ampersand character. Do I need to escape this character somehow?



That depends.

If the code is embedded in an HTML page, then the ampersand should be
escaped (as an HTML entity, "&amp;", not a Javascript string escape
"\&"). If it is in an external javascript file, then it should not.

So, probably. Have you tried escaping it?

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''


Lasse Reichstein Nielsen hu kiteb:

However, when I parse the url, it consistently chokes when presented
with an ampersand character. Do I need to escape this character
somehow?



That depends.

If the code is embedded in an HTML page, then the ampersand should be
escaped (as an HTML entity, "&amp;", not a Javascript string escape
"\&"). If it is in an external javascript file, then it should not.

So, probably. Have you tried escaping it?



It is in an external javascript file. I have tried escaping it, and none
of the escape sequences I know seem to make much different. I''ve tried
%26, , &amp;, and \u0026. I''m not aware of other methods for
escaping the character.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk


这篇关于网址解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆