How can one identify the currently clicked link with Javascript?

假如想象 提交于 2019-12-11 23:57:17

问题


I am using Internet Explorer.

I added a context menu item (through the registry) such that when right clicking on a link in a webpage a custom menu item pops up. Upon selection, this menu item runs some javascript code.

I want to use the url of the link (on which I right click) in the javascript code - how do I access that url?

*Note that this should work for any webpage, not only ones which I have control over.

Thanks in advance!


回答1:


<script language="JavaScript">
  var parentwin = external.menuArguments;
  var doc = parentwin.document;
  var url = doc.URL;

  // ... the rest of your code here ...

See also.

To get the source object, try:

<script language="JavaScript">
  var parentwin = external.menuArguments;
  var srcElement = parentwin.event.srcElement;

  if (srcElement.tagName == "A") {
    var url = srcElement.href;

    // ... the rest of your code here ...
  }


来源:https://stackoverflow.com/questions/7237870/how-can-one-identify-the-currently-clicked-link-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!