简体   繁体   English

为什么在锚标签的 href 属性中使用“javascript:function()”来调用 javascript function

[英]Why "javascript:function()" is used in href attribute of anchor tag to call an javascript function

I have my code like this and I want to call an JavaScript function which returns an website link.我有这样的代码,我想调用 JavaScript function 返回网站链接。 Which link I want to be the value of href="" attribute.我希望哪个链接成为 href="" 属性的值。 So somewhere I got the solution to do so.所以在某个地方我得到了这样做的解决方案。 But there wasn't defined that why to do so.但是没有定义为什么要这样做。 Here's my code.这是我的代码。 I want to know why to call javascript function inside href attribute href="javascript:functionname()" is to be written rather than href="functionname()".我想知道为什么调用javascript function里面的href属性要写成href="javascript:functionname()"而不是href="functionname()"。

 function hyperlinker(){ document.location.href="https://www.google.co.in"; }
 <a href="javascript:hyperlinker()"> click to get new page </a>

Having an anchor like有一个锚点

<a href="somefunc()">Click Me</a>

Would be interpreted as "try to navigate to the route somefunc()".将被解释为“尝试导航到路由 somefunc()”。 So you would be sent to http://www.example.com/somefunc() .所以你会被发送到http://www.example.com/somefunc() Where http: is the protocol of the current page you're on.其中http:是您当前所在页面的协议。

The javascript: , much like the http: and the mailto: and the tel: as well as any other prefix like that is the protocol to use . javascript:很像http:mailto:tel:以及任何其他类似的前缀是要使用的协议 If you leave it off, you're leaving it up to the browser's default behavior.如果您将其关闭,则将其留给浏览器的默认行为。 For relative URLs (which is what you have written), it will default to http: (Thanks Barmar)对于相对 URL(这是您所写的), 它将默认为http:感谢 Barmar)

You don't need to include the protocol (the browser uses HTTP by default) or the port (which is only required when the targeted Web server is using some unusual port), but all the other parts of the URL are necessary.您不需要包括协议(默认情况下浏览器使用 HTTP)或端口(仅当目标 Web 服务器使用一些不寻常的端口时才需要),但 URL 的所有其他部分都是必需的。

See MDN <a/> Tag href :参见MDN <a/>标签href

href

The URL that the hyperlink points to.超链接指向的 URL。 Links are not restricted to HTTP-based URLs — they can use any URL scheme supported by browsers:链接不限于基于 HTTP 的 URL——它们可以使用浏览器支持的任何 URL 方案:

  • Sections of a page with fragment URLs包含片段 URL 的页面部分
  • Pieces of media files with media fragments带有媒体片段的媒体文件
  • Telephone numbers with tel: URLs带有 tel: URL 的电话号码
  • Email addresses with mailto: URLs Email 地址与 mailto: URLs
  • While web browsers may not support other URL schemes, web sites can with registerProtocolHandler()虽然 web 浏览器可能不支持其他 URL 方案,但 web 站点可以使用 registerProtocolHandler()

If your intention is to call some JavaScript function though, you shouldn't use href anyway, that's what onclick is for, or better still addEventListener() and if the anchor won't be "taking you anywhere", use a <button/> for accessibility.如果你打算打电话给一些 JavaScript function,你不应该使用href ,这就是onclick的用途,或者更好的是addEventListener()并且如果锚点不会“带你去任何地方”,请使用<button/>无障碍。

Anchor elements are often abused as fake buttons by setting their href to # or javascript:void(0) to prevent the page from refreshing, then listening for their click events.锚元素经常被滥用为假按钮,通过将它们的href设置为#javascript:void(0)来阻止页面刷新,然后监听它们的click事件。

These bogus href values cause unexpected behavior when copying/dragging links, opening links in a new tab/window, bookmarking, or when JavaScript is loading, errors, or is disabled.这些伪造的href值会在复制/拖动链接、在新选项卡/窗口中打开链接、添加书签或 JavaScript 正在加载、出错或被禁用时导致意外行为。 They also convey incorrect semantics to assistive technologies, like screen readers.它们还会将不正确的语义传达给屏幕阅读器等辅助技术。

Use a <button> instead.请改用<button> In general, you should only use a hyperlink for navigation to a real URL.通常,您应该只使用超链接导航到真实的 URL。

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

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