简体   繁体   English

为什么在函数名称之前需要写“ javascript:”?

[英]Why do I need to write “javascript:” before a function name?

If I want to use an external javascipt function like "functionname()", see the example below: 如果要使用外部Javascipt函数(例如“ functionname()”),请参见以下示例:

<from onSubmit="javascript: funtionname()" >
</form>

why do I need sometimes to write javascript: before the function name? 为什么有时需要在函数名称前写javascript :?

In handlers ( onclick , onchange , etc.), you don't have to use the prefix. 在处理程序( onclickonchange等)中,您不必使用前缀。 For these, Javascript is already expected and the prefix is interpreted as a label and has no effect. 对于这些,已经期望使用Javascript,并且前缀将被解释为标签,并且无效。

If you want to execute Javascript in an href attribute for example, the prefix "javascript:" is needed. 例如,如果要在href属性中执行Javascript, 需要前缀“ javascript:”。 Otherwise the value would be interpreted as a URL. 否则,该值将被解释为URL。

In general, though, this practice should be avoided as it contradicts unobtrusive Javascript policy. 但是,一般而言,应避免这种做法,因为它与不干扰Javascript策略的行为相矛盾。

The javascript: notation is a URL scheme, and may be used wherever a URL is expected. javascript:表示法是URL方案,可以在需要URL的任何地方使用。 As far as I am aware, passing a URL to an event handler is incorrect (at least according to the HTML5 spec) though browsers appear to support it. 据我所知,尽管浏览器似乎支持将URL传递给事件处理程序是不正确的(至少根据HTML5规范)。 You are expected to specify a valid function or null as an event handler. 您应该指定一个有效的函数或将null指定为事件处理程序。 The browser may be interpreting this javascript prefix as a label, though it might be technically incorrect to allow this. 浏览器可能将此javascript前缀解释为标签,尽管从技术上讲不允许这样做。

You don't. 你不知道

You will need to write javascript: when you using an url type tag only (like: href and src ), to specify that the string need to eval a javascript code. 您将只需要编写javascript:当您仅使用url类型标记(例如: hrefsrc )时,指定该字符串需要评估javascript代码。

Example: 例:

<a href="javascript: doSomething();">
    My link is a javascript function doSomething()
</a>

You can: 您可以:

<form onSubmit="doSomething();">
    When submit the form, will ran doSomething, 
    if returns false will cancel the form submission
</form>

No need to do it in handlers. 无需在处理程序中执行此操作。 It is used only as protocol in URL-like method, eg: 它仅用作类似URL的方法中的协议,例如:

<a href="javascript:funtionname()">Click here</a>

暂无
暂无

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

相关问题 为什么在 javascript 中使用 onkeydown 时,function() 需要在 function 名称之前键入? - why does function() need to be typed before the function name when using onkeydown in javascript? (javascript)为什么我需要为事件处理程序使用wrap函数? - (javascript) why do i need to use a wrap function for event handlers? 如果对该函数的调用位于javascript的同一模块中,那么我需要先定义一个函数才能执行该函数吗? - Do I need to define a function before it can be execute if the call to the function is in the same module in javascript? 为什么我需要在node.js中编写“function(value){return my_function(value);}”作为回调? - Why do I need to write “function(value) {return my_function(value);}” as a callback in node.js? 为什么在从Firebase加载数据之前需要运行两次此功能? - Why do I need to run this function twice before it loads my data in from firebase? 为什么我需要在 function 结束之前放置一个 return 语句? - why do I need to put a return statement right before a function ends? onChange function 一键停止,我需要写什么代码才能无限期地继续? (Javascript) - onChange function stops after one click, what code do I need to write so that it continues indefinitely? (Javascript) Javascript:如果已经使用外部函数,为什么需要定义自调用匿名函数? - Javascript: Why do I need to define self invoking anonymous function if I'm already using an external function? 为什么在构造函数中的方法之前需要“this”? - Why do you need 'this' before a method in a constructor function? 我需要在javascript变量名之前加一美元吗? - Do I need to put a dollar before variable names with javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM