Javascript HTML onload - 使用变量作为参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4003345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:05:55  来源:igfitidea点击:

HTML onload - using variables as parameters

javascripthtmlvariablesparameter-passingonload

提问by Greg B

I want to use something like:

我想使用类似的东西:

<body onLoad="init('A sentence with "quoted text" as parameter')">

Unfortunately, this does work, as the quotes in the parameter are not treated properly.

不幸的是,这确实有效,因为参数中的引号没有得到正确处理。

Escaping the quotes also does not work

转义引号也不起作用

<body onLoad="init('A sentence with \"quoted text\" as parameter')">

(Above also does not work).

(以上也不起作用)。

How do I deal with this. I though maybe I can create a string variable and assigne my sentence (with quotes) to it. But I dont know how to do it! The body onload is HTML and the Javascript variables would be visible only within the scope of the script, right? To be precise, the following does not work:

我该如何处理。我虽然也许我可以创建一个字符串变量并将我的句子(带引号)分配给它。但我不知道该怎么做!主体 onload 是 HTML 并且 Javascript 变量仅在脚本范围内可见,对吗?准确地说,以下方法不起作用:

<script language="JavaScript">
var dada='A sentence with \"quoted text\" as parameter';
</script>
<body onLoad="init($dada, '</a>')">

回答by Pekka

You would have to use HTML entities to make it work:

您必须使用 HTML 实体才能使其工作:

<body onLoad="init('A sentence with &quot;quoted text&quot; as parameter')">

the much cleaner way, though, would be to assign the value in a separate <SCRIPT>part in the document's head.

不过,更简洁的方法<SCRIPT>是在文档头部的单独部分中分配值。

...
<script>
body.onload = function() { init('A sentence with "quoted text" as parameter'); }
</script>
<body>
...

the onloadevent has the general downside that it is fired only when the document and all its assets(images, style sheets...) have been loaded. This is where the onDOMLoad event comes in: It fires when the core HTML structure is ready, and all elements are rendered. It is not uniformly supported across browsers, though, so all frameworks have their own implementation of it.

onload事件有一个普遍的缺点,即只有在加载了文档及其所有资产(图像、样式表...)时才会触发。这就是 onDOMLoad 事件的用武之地:它在核心 HTML 结构准备就绪并呈现所有元素时触发。但是,它在浏览器之间并没有得到统一支持,因此所有框架都有自己的实现。

The jQuery version is called .ready().

jQuery 版本称为.ready().

回答by Greg B

Rather than inlining the onLoadmethod, why not set it programatically. You can then use the power of closuresto get the data into the function.

与其内联onLoad方法,不如以编程方式设置它。然后,您可以使用闭包的强大功能将数据放入函数中。

<script type="text/javascript">
var data = "some data";
document.body.onload = function()
{
    alert(data);
}
</script>

回答by Grumpy

not the nicest way

不是最好的方式

onload='init("A sentence with \"quoted text\" as parameter")'

回答by Grumpy

By the way, I was making a silly mistake.

顺便说一下,我犯了一个愚蠢的错误。

<script language="JavaScript"> 
var dada='A sentence with \"quoted text\" as parameter'; </script> 
<body onLoad="init(dada, '</a>')"> 

This works too. I was using $dada instead of dada in the onload call.

这也有效。我在 onload 调用中使用 $dada 而不是dada。