简体   繁体   English

javascript:Function语法是什么意思?

[英]What does javascript:Function syntax mean?

Check here 在这里检查

javascript:Function("alert('this is ok')")();

same as 如同

new Function("alert('this is ok')")();

What the syntax called ? 语法叫什么?

Any reference ? 有参考吗?

EDIT 编辑

Sorry, my fault,it's only a label,and it's not equals new . 抱歉,我的错,它只是一个标签,并不等于new

<script>
    javascript:C("val");
    wener:C("val");
    function C(v)
    {
        this.val = v;
        alert(window.val)
    }
</script>
<a href="javascript: alert('foo');">click me</a>

For <a href="javascript: alert('foo');">click me</a> see URI_scheme (Thanks apsillers) and draft-hoehrmann-javascript-scheme 对于<a href="javascript: alert('foo');">click me</a>请参阅URI_scheme (感谢ps徒)和草稿hoehrmann-javascript-scheme

It's called "the old way of using JavaScript in attributes," relevant only in HTML. 它被称为“在属性中使用JavaScript的旧方法”,仅与HTML有关。

You can also write: 您还可以编写:

poop:Function("alert('ohai')")();

Your example is just JavaScript code, not an HTML attribute, so it's just a label. 您的示例只是JavaScript代码,而不是HTML属性,因此它只是一个标签。

When you specify a fully qualified link, the text to the left of the colon is called the scheme . 指定完全限定的链接时,冒号左侧的文本称为scheme

<a href="https://example.com/foo">foo</a>
           ^ scheme is "https"

Browsers allow you the specify a scheme of javascript in HTML markup, in which case the browser interprets the link content to the right of the colon as a script to be executed, instead of a destination resource. 浏览器允许您在HTML标记中指定javascript方案,在这种情况下,浏览器会将冒号右侧的链接内容解释为要执行的脚本,而不是目标资源。

<a href="javascript:alert('I am a script, formatted as a link!')">foo</a>
            ^ scheme is "javascript"

(Please, never do this; use addEventListener("click", function() { ... }) instead.) (请不要这样做;请改用addEventListener("click", function() { ... }) 。)

If you include the javascript: prefix in non-link contexts, however, it's interpreted as a JavaScript label . 但是,如果您在非链接上下文中包含javascript:前缀,则会将其解释为JavaScript标签 This is totally unnecessary. 这完全没有必要。

This approach might be used when you want to execute JavaScript in browser's address bar. 当您想在浏览器的地址栏中执行JavaScript时,可以使用这种方法。 Copy/paste javascript:Function("alert('this is ok')")(); 复制/粘贴javascript:Function("alert('this is ok')")(); into browser's address bar and hit Enter. 进入浏览器的地址栏,然后按Enter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM