右键单击href javascript函数不起作用 [英] On right click of href javascript function not working
问题描述
当我右键点击Href并选择在新标签页中打开时,此javascript代码无效。
Href代码是
This javascript code is not working when I right click on Href and select open in new tab.
Href code is
a href='javascript:relativeURI("//my.barcapint.com/BC/barcaplive?url=%2Fcci%2Ffirm%2Ftopics%2FFalcon%2F&LLPreviousMenuCode=INT_TOPICS");'>
< br $>
function relativeURI(uriString) {
var protocal = window.location.protocol;
var filePath = protocal + uriString;
window.open(filePath, '_blank');
}
请建议。
我尝试过:
无法解决右键单击问题。代码不能用于IE和chrome
please suggest.
What I have tried:
Not able to sort out for right click. Code is not working on IE and chrome
推荐答案
它无效,因为你在<$ c中使用javascript
$ c> Href ,只有当href
具有有效<时,右键单击才能工作(导航到确切路径) code> url 。
它可以通过多种方式修复,这是其中之一,如果你使用jquery它会更容易。
Its not working because you are usingjavascript
inHref
, right click will work (navigate to exact path) only when thehref
has an validurl
.
It can be fixed in many ways, this is one of them, if you are using jquery it will be much more easier.
function HandleRightClick(obj, uriString)
{
// alert('right click');
var protocal = window.location.protocol;
var filePath = protocal + uriString;
obj.href = filePath; // assign the url to href attribute
}
<a id="myAnchor" oncontextmenu="HandleRightClick(this,'//my.barcapint.com/BC/barcaplive?url=%2Fcci%2Ffirm%2Ftopics%2FFalcon%2F&LLPreviousMenuCode=INT_TOPICS')" onclick="javascript: relativeURI("//my.barcapint.com/BC/barcaplive?url=%2Fcci%2Ffirm%2Ftopics%2FFalcon%2F&LLPreviousMenuCode=INT_TOPICS");" href=""> test</a>
在这种情况下,您不需要Javascript at all:
In this case, you don't need the Javascript at all:
<a href="//my.barcapint.com/..." target="_blank" rel="noreferrer noopener">
这称为协议相对网址:
< a href =https://www.paulirish.com/2010/the-protocol-relative-url/>协议相对网址 - 保罗爱尔兰 [ ^ ]
< b>注意:正如文章中提到的,现在这被认为是一种反模式。如果链接资源可通过SSL获得,则应始终链接到安全版本:
This is known as a protocol-relative URL:
The protocol-relative URL - Paul Irish[^]
NB: As mentioned in the article, this is now considered an anti-pattern. If the linked resource is available over SSL, then you should always link to the secure version:
<a href="https://my.barcapint.com/..." target="_blank" rel="noreferrer noopener">
这篇关于右键单击href javascript函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!