检测浏览器 [英] detecting browser

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

问题描述



请问,为什么这不起作用?


< SCRIPT language = JavaScript type =" text / javascript">

var br =''< SCRIPT language = Javascript''

br + =''src =" js_pop.js" type =" text / javascript">''

br + =''< / SCRIPT>''

var op =''< SCRIPT language = Javascript''

op + =''src =" js_pop-op.js" type =" text / javascript">''

op + =''< / SCRIPT>''

if(navigator.userAgent.indexOf(''' Opera'')!= -1){

document.write(op)

} else {

document.write(br)< br $>
}

// - >

< / SCRIPT>


我拉了一个JS圣经中具有相同结构的脚本:


函数重写(){

var newContent = a ??< HTML>< HEAD>< ; TITLE>新文件< / TITLE>< / HEAD> a ??

newContent + = a ??< BODY BGCOLOR = a ?? aquaa ??>< H1>这文件是全新的。< / H1> a ??

newContent + = a ??点击后退按钮查看原始文件.a ??

newContent + = a ??< / BODY>< / HTML> a ??

document.write(newContent)

}

什么''我的建筑错了?错误消息我在IE中说

未终止的字符串常量..而另一个奇怪的是该部分

的脚本出现在页面的最顶部,这就是出现的情况:


" var op ="

http:// www.francesdelrio.com/hbl/index2.html

感谢您的帮助....


Frances


pls, why is this not working?

<SCRIPT language=JavaScript type="text/javascript">
var br = ''<SCRIPT language=Javascript''
br += ''src="js_pop.js" type="text/javascript">''
br += ''</SCRIPT>''
var op = ''<SCRIPT language=Javascript''
op += ''src="js_pop-op.js" type="text/javascript">''
op += ''</SCRIPT>''
if (navigator.userAgent.indexOf(''Opera'') != -1 ) {
document.write(op)
} else {
document.write(br)
}
//-->
</SCRIPT>

I pulled a script with an identical construction from the JS Bible:

function reWrite() {
var newContent = a??<HTML><HEAD><TITLE>A New Doc</TITLE></HEAD>a??
newContent += a??<BODY BGCOLOR=a??aquaa??><H1>This document is brand new.</H1>a??
newContent += a??Click the Back button to see original document.a??
newContent += a??</BODY></HTML>a??
document.write(newContent)
}
what''s wrong with my construction?? error msg I get in IE says
"undeterminated string constant".. and the other weird thing is that part
of the script appears at the very top of the page, this is what appears:

" var op = "
(http://www.francesdelrio.com/hbl/index2.html)
thank your for any help....

Frances

推荐答案

>请问,为什么这不起作用?
> pls, why is this not working?

< SCRIPT language = JavaScript type =" text / javascript">
var br =''< SCRIPT language = Javascript''
br + =''src =" js_pop.js" type =" text / javascript">''
br + =''< / SCRIPT>''
var op =''< SCRIPT language = Javascript''
op + =''src =" js_pop-op.js" type =" text / javascript">''
op + =''< / SCRIPT>''
if(navigator.userAgent.indexOf(''Opera'')!= -1 ){
document.write(op)
} else {
document.write(br)
}
// - >
< / SCRIPT>

我从JS圣经中提取了一个具有相同结构的脚本:

函数reWrite(){
var newContent = a ??< HTML> ;< HEAD>< TITLE>新文件< / TITLE>< / HEAD> a ??
newContent + = a ??< BODY BGCOLOR = a ?? aquaa ??>< H1> ;这个文件是全新的。< / H1> a ??
newContent + = a ??点击Back按钮查看原始文件.a ??
newContent + = a ??< / BODY>< / HTML> a ??
document.write(newContent)
}
我的构造有什么问题?错误消息我在IE中说
未终止的字符串常量...而另一个奇怪的是脚本的部分
出现在页面的最顶部,这就是出现的内容:

var op ="

<SCRIPT language=JavaScript type="text/javascript">
var br = ''<SCRIPT language=Javascript''
br += ''src="js_pop.js" type="text/javascript">''
br += ''</SCRIPT>''
var op = ''<SCRIPT language=Javascript''
op += ''src="js_pop-op.js" type="text/javascript">''
op += ''</SCRIPT>''
if (navigator.userAgent.indexOf(''Opera'') != -1 ) {
document.write(op)
} else {
document.write(br)
}
//-->
</SCRIPT>

I pulled a script with an identical construction from the JS Bible:

function reWrite() {
var newContent = a??<HTML><HEAD><TITLE>A New Doc</TITLE></HEAD>a??
newContent += a??<BODY BGCOLOR=a??aquaa??><H1>This document is brand new.</H1>a??
newContent += a??Click the Back button to see original document.a??
newContent += a??</BODY></HTML>a??
document.write(newContent)
}
what''s wrong with my construction?? error msg I get in IE says
"undeterminated string constant".. and the other weird thing is that part
of the script appears at the very top of the page, this is what appears:

" var op = "




当浏览器看到


br + =''< / SCRIPT>''


它愚蠢地认为< / SCRIPT>是脚本的结尾。那么
看到的编译器是什么?


br + =''


看起来像一个未终止的字符串常量并且脚本的其余部分是以文本形式显示的
。这样做的道理是你必须弄清楚关闭脚本

标签,这样浏览器就不会那么愚蠢。我建议在>之前插入\

。其他人喜欢把它放在斜线之前。我认为最好将

放在>之前。因为这也解决了XML CDATA的相关问题。


br + =''< / script\>'';


jslint检查这个。 http://www.crockford.com/javascript/lint.html


摆脱// - >。这是浪费空间。最新款式是在标签中使用

小写。



When the browser sees

br += ''</SCRIPT>''

it stupidly thinks that </SCRIPT> is the end of the script. So what the compiler
sees is

br += ''

which looks like an unterminated string constant and the rest of the script is
displayed as text. The moral of that is that you must dirty up the close script
tag so that the browser will not behave so stupidly. I recommend inserting a \
before the >. Others like to put it before the slash. I think it is better to
put it before the > because that also solves a related problem with XML CDATA.

br += ''</script\>'';

jslint checks for this. http://www.crockford.com/javascript/lint.html

Get rid of the //-->. It is a waste of space. The latest style is to use
lowercase in the tags.


" Frances Del Rio" < FD *** @ yahoo.com>在消息中写道

news:op ************** @ news.cis.dfn.de ...

当网络浏览器找到一个开头脚本标签,如: -
"Frances Del Rio" <fd***@yahoo.com> wrote in message
news:op**************@news.cis.dfn.de...
When a web browsers finds an opening script tag like:-
< SCRIPT language = JavaScript type =" text / javascript">


它必须决定发送给JavaScript

解释器后面的内容。它通过寻找 - < / SCRIPT>来做到这一点。 - 在HTML中。

var br =''< SCRIPT language = Javascript''
br + =''src =" js_pop.js" type =" text / javascript">''


它在此处找到: -

br + =''< / SCRIPT>' '
<SCRIPT language=JavaScript type="text/javascript">
It has to decide how much of what follows to send to the JavaScript
interpreter. It does that by looking for - </SCRIPT> - in the HTML.
var br = ''<SCRIPT language=Javascript''
br += ''src="js_pop.js" type="text/javascript">''
And it finds it here:-
br += ''</SCRIPT>''




要在JavaScript字符串中使用结束HTML脚本标记,您需要

使其看起来像其他内容。使用转义字符可以

有效 - < \ / script> - 或< / script\> - 。


< snip>


Richard。



To use the closing HTML script tag in a JavaScript string you need to
make it look like something else. Using escape characters can be
effective - <\/script> - or </script\> -.

<snip>

Richard.


7月2日2003 13:26:19 +0200,Lasse Reichstein Nielsen

< lr*@hotpop.com>写道:
On 02 Jul 2003 13:26:19 +0200, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
" Douglas Crockford" <无**** @ laserlink.net>写道:
"Douglas Crockford" <no****@laserlink.net> writes:
其他人喜欢把它放在斜线之前。我认为最好把它放在>之前。因为这也解决了XML CDATA的相关问题。

br + =''< / script\>'';
Others like to put it before the slash. I think it is better to
put it before the > because that also solves a related problem with XML CDATA.

br += ''</script\>'';



最令人惊讶的事情就是它有效。根据规范,终止脚本内容的是结束标记打开,即< /。



The most suprising thing is that it works. According to specification,
what terminates the script content is the "end tag open", i.e., "</".




我认为它的工作原理是因为浏览器中的纠错,他们看到了ETAGO,但是因为它没有跟着SCRIPT>然后他们知道

这不是作者的意图。


吉姆。

-

comp.lang.javascript常见问题 - http://jibbering.com/faq/



I think it works because of error correction in the browser, they see
the ETAGO, but because it''s not followed by SCRIPT> then they know
it''s not what the author intended.

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/


这篇关于检测浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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