本文介绍了为什么不是铬运行此JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Chrome(版本2)中运行它时,它不会返回字符串,但它可以在Firefox(版本3)中运行。为什么会这样?

 < html> 
< head>
< script type =text / javascript>
函数disp_prompt(){
var name = prompt(请输入你的名字,哈利波特);
if(name!= null&& name!=){
document.write(Hello+ name +!今日过得如何?
}
}
< / script>
< / head>
< body>

< input type =buttononclick =disp_prompt()value =显示提示框/>

< / body>
< / html>


解决方案

要将内容附加到文档, document.write()在< script> -block分析阶段。



如果您打电话给 document.write 在某处执行该上下文之外的地方,比如在你的事件处理程序中,没有进行解析,所以无法为该字符串' foo'去。



根据是:

也就是说,它假定你犯了一个错误,并且意味着要做:

  document.open(的text / html); 
document.write(html);

用新内容替换整个文档。您还会注意到,因为您没有通过调用完成它:

  document.close(); 

...在Firefox中,'loading'throbber仍然具有动画效果,期待更多内容被写入(但它永远不会出现)。

在这种情况下,您是 document.write() ing一个字符串:

 您好bobince!你今天好吗? 

这显然不是一个有效的HTML文档。 Chrome(和Safari,其行为相同)试图将其解析为HTML文档,但因文档元素之外的非空白文本内容(< html>)而失败,这是不允许的。如果您在名称输入中添加了一些标签:

  Hello< em> bobince< / em> ;!你今天好吗? 

您会看到< em>元素之外的所有内容都被丢弃。 Firefox和IE设法应用他们的修正文本以外的任何元素破坏HTML规则来拯救你,但Chrome不会。



如果你想 document.write()一个新文档,您应该确保它确实是一个完整的文档:

 < html>< head>< title>问候语< / title>< body>< p>您好! < / body>< / html>你今天过得如何?< / p>< 

或者,如果您想要 document.write()一个文本字符串,理论上你应该调用:

  document.open('text / plain'); ()之前的

。但要注意,Chrome或Safari不支持mimetype参数;他们会一直把你写的东西当作HTML。



在任何情况下,你可能都不想使用这种方法。如果您想要添加(或更改)现有页面,通常应该使用DOM方法,例如在状态输出div上设置文本,或使用innerHTML替换正文内容。


$ b在一些地方需要使用$ b

document.write()(特别是当您从头开始创建新窗口或框架时),但除此之外通常错误的东西,应该被怀疑地看待。


When I run this in Chrome (ver 2), it doesn't return the string, but it works in Firefox (ver 3). Why is this?

<html>
<head>
<script type="text/javascript">
function disp_prompt() {
    var name=prompt("Please enter your name","Harry Potter");
    if (name!=null && name!="") {
        document.write("Hello " + name + "! How are you today?");
    }
}
</script>
</head>
<body>

<input type="button" onclick="disp_prompt()" value="Display a prompt box" />

</body>
</html>
解决方案

To append content to a document, you should only call document.write() during the <script>-block parsing phase.

If you have a call to document.write somewhere where it executes outside that context, such as in your event handler, there is no parsing going on, so nowhere for that string 'foo' to go.

What should happen according to the original JavaScript reference is:

That is, it assumes you've made a mistake and meant to do:

document.open('text/html');
document.write(html);

Which replaces the entire document with new content. You'll also notice that because you didn't finish that off by calling:

document.close();

...in Firefox, the ‘loading’ throbber is still animating, expecting more content to be written to the document (but it never comes).

In this case, you are document.write()ing a string:

Hello bobince! How are you today?

that is clearly not a valid HTML document. Chrome (and Safari, which behaves the same) is trying to parse it as an HTML document, but fails because it is non-whitespace text content outside the document element (<html>), which is not allowed. If you put some tags in the name input:

Hello <em>bobince</em>! How are you today?

You'll see everything outside the <em> element is discarded. Firefox and IE manage to apply their "fix up text outside any element" broken-HTML-rule to save you, but Chrome doesn't.

If you want to document.write() a new document, you should make sure it is indeed a complete document:

<html><head><title>Greetings</title><body><p>Hello bobince! How are you today?</p></body></html>

Or, if you want to document.write() a text string, you should theoretically call:

document.open('text/plain');

before the write(). But beware, the mimetype parameter is not supported by Chrome or Safari; they'll always treat what you write as HTML.

In any case, you probably don't want to be using this method. If you want to add to (or change) the existing page you should normally be using DOM methods, such as setting the text on a status output div, or replacing the body contents using innerHTML.

document.write() is needed in a few places (in particular, when you are creating a new window or frame from scratch), but otherwise it's generally the wrong thing and should be viewed with suspicion.

这篇关于为什么不是铬运行此JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 21:19
查看更多