English 中文(简体)
html - 如何防止浏览器打开红外线?
原标题:html - How to prevent the browser from opening the link specified in href?

I currently making a filebrowser. If the user clicks on a link to a file a little window open and asks for options (like download and view). I ve done this using the onclick attribute. If I click on the link the javascript is executed, but after this the url specified in href opens. What I m trying to do is: If you click the link javascript get executed and forwards you eventually. But if the link gets rightclicked the "copy link location" should still be available. I m thinking of solving this by blocking the forwarding scriptside. So if the link gets rightclicked no javascript is executed and you can copy the link location. But if you left click the link the javascript is executed and the link is not opened. Is that possible with javascript, or is there any other way of achieving this behavior?

最佳回答

In order to prevent the link executing, one way is to let the "onclick" method return false.

例如:

<a href="http://..." onclick="return clickfunc()">...</a>

如果clickfunc( Returncode>>false,则点击该链接不会直接到“http://. >。

虽然你可能更喜欢处理。

<a href="http://..." id="somebutton">...</a>
<script>
document.getElementById( somebutton ).onclick = function() { 
      ... 
      else { return false; }
};
</script>
问题回答

你必须防止你点击职能出现违约情况:

function stopDefAction(evt) {  
  evt.preventDefault();  
} 

是的,你需要,以预先防范该活动的默认行动。 你可以通过将<代码>false填入你的立体。

您可以用“javascript:ave(0)”文本取代“href属性”,例如:

<a href="http://..." id="somebutton">...</a>
<script>
document.getElementById( somebutton ).href="javascript:void(0)"
</script>




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签