How do I disable/remove the 'Open Link in New Tab' option in the right-click menu of the browser? If this is not possible with javascript etc then is there a way to modify what happens when the user clicks on this link, for example, display an alert or prevent the tab from loading..?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
I am not sure why would some one downvote your question, what you need to google is how to hijack events using javascript. You can very well show an alert when you click a link and stop it from working. jQuery is a populat JS toolkit and has a method to stop default events on an element, in plain English, using jQuery you can stop a hyperlink from working it and show an alert as well. This link http://api.jquery.com/event.preventDefault/ can help you further. Regarding disabling the context menu that appears on right clicking on a webpage, yes that is very much possible. I am not a JS ninja so can't comment if you can remove an item from context menu but IMO, it should not be possible.
2019
I'll highlight 2 important comments:
nnnnnn @ Aug 10 '11 at 5:49 :
Pars @ Dec 21 '13 at 13:10 :
It's up to the browser to decide whether it gets the link-behavior or not.
Some modern browser (like Chrome) will consider it a link if all the below conditions matches:
<a>...</a>
tag (notspan
,div
,button
, etc).href
attribute.href
attribute is not empty.The below will get the link-behavior:
The below will NOT get the link-behavior:
Important Note:
Again, it's up to the browser to decide. Some old/stupid browsers may give it link-behavior if ANY of the 3 conditions above matches!
Conclusion
If it's a link then use
<a href="some_link">...</a>
. If it's not a link then use something else, like<button>
.The ability to open a link in a new tab/window is native functionality of many browsers. If you do not wish to allow this type of activity, then you need to notify the browser that your link is not truly a link. The easiest way to do so is to remove the
href
attribute from youra
element.HTML:
Now there are some other things that the browser may be doing for you by default when it sees a link. If you have not defined any styling of
a
elements, it's likely that your new fancy pseudo-link will not appear with a link font-color, pointer, and underline. You can go ahead and do that easily enough.CSS:
That hopefully answers the question of how you disable/remove the 'Open Link in New Tab' option in the right-click menu of the browser. For some extra credit though I'm going to assume that you probably want the link to still function like a regular link when clicked. Feel free to use some JavaScript to make that happen. Here's an example using jQuery:
JavaScript:
Modified Html:
Modified CSS:
Hope this helps!
Have solved the main issue now, quite easily as it turned out just by passing the query string of the href to an ajax function.
I probably should have explained in my question that what I wanted was intended only as a temporary measure. Anyway thanks for all the comments.