简体   繁体   English

禁止JavaScript在链接上运行

[英]disable javascript from running on links

I have a script that reloads an iframe whenever the browser screen is clicked (works perfectly). 我有一个脚本,只要单击浏览器屏幕,它就会重新加载iframe(效果很好)。 I was wondering how I would go about disabling the javascript from running on certain links? 我想知道如何禁止在某些链接上运行javascript?

Here is my current script: 这是我当前的脚本:

document.onclick= function(event) {
    if (event===tichack) event= window.event;
    var target= 'target' in event? event.target : event.srcElement;

   document.getElementById("tichack").src = "http://link.com?"+(+new Date());
};

Example would be something like this i suppose? 我想例子是这样的吗? (obviously incorrect) but will give you a better idea of what I am trying to achieve, did allot of searching but have had no luck?: (显然是错误的),但可以让您更好地了解我要达到的目标,是否分配了搜索但没有运气?:

onclick="javascript:disabled=true;"

Basically, you need to identify what the target of your click is and, if the target is equal to one of the links you don't want triggering this, return from the function without refreshing the iframe. 基本上,您需要确定点击的目标是什么,并且如果目标等于您不希望触发该链接的链接之一,则无需刷新iframe就可以从函数中返回。

var links = /* a collection of elements you don't want triggering the iframe refresh */,
    count = links.length,
    i;

for (i = 0; i < count; i += 1) {
    if (target === links[i]) {
        return;
    }
}

You can do this: 你可以这样做:

document.onclick = function (event) {
    event = event || window.event;
    if (event.target.id != 'tichack') return;
    document.getElementById("tichack").src = "http://link.com?" + (+new Date());
};

Here, the document click event will work for the tichack link only. 在这里,文档单击事件仅适用于tichack链接。

SIMPLE DEMO HERE 这里简单的演示

There's two ways: 有两种方法:

  • in the document.onclick, check the target element if it's a certain link, and then don't do anything. 在document.onclick中,检查目标元素是否为特定链接,然后不执行任何操作。 Ie if (target.id == '...') { return; } if (target.id == '...') { return; } if (target.id == '...') { return; }
  • make sure that the click never arrives at the document.onclick. 确保单击永远不会到达document.onclick。 Because of how the DOM Event model works, if you cancel an onclick event at a low level (ie at a link itself) it will not bubble up and arrive at the document.onclick. 由于DOM事件模型的工作原理,如果您在较低级别(即,链接本身)取消onclick事件,它将不会冒泡并到达document.onclick。 So the following code works: 因此,以下代码有效:

    <a href='...' onclick='return false;'>click here!</a>

See http://bitovi.com/blog/2010/10/a-crash-course-in-how-dom-events-work.html for more information on the DOM Event model! 有关DOM事件模型的更多信息,请参见http://bitovi.com/blog/2010/10/a-crash-course-in-how-dom-events-work.html

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

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