I need a pure Javascript code to disable all links while loading the page, avoiding to click them and redirecting to other page when its using a Facebox.
Can be something that I place in a <script> tag in the <head>.
Any suggestions?
EDIT:
Ok, I get that in head doesn't work. But in body ending work properly. That's why I asked for suggestions. :)
Make this the first script tag in your head section:
window.onclick = function(e) {
if(typeof $ === 'undefined') {
e = e || window.event;
e.preventDefault();
}
}
This catches all click events, and if $ is undefined, cancels the event.
Has the advantage of working even before the DOM is loaded.
Use document.links and prevent their default behaviour with preventDefault().
var links = document.links;
for (var i = 0, length = links.length; i < length; i++) {
links[i].onclick = function(e) {
e = e || window.event;
e.preventDefault();
}
}
If you wanted a more permanent disabling of the links, use removeAttribute('href').
Place this code somewhere after the closing body tag if you don't want to use DOMContentLoaded or the window load event.
You can't place it in the head element otherwise and have it execute immediately because document.links will most certainly be empty.
In the page, give all your links empty href values. Use the load event to assign appropriate values. Users with javascript disabled or unavailable will see broken links.
Related
I'm trying to create a userscript that causes the native popup blocker to apply to all popups, even those that were the result of user interaction.
I came up with the following idea:
window.addEventListener('click', function(e) {
console.log(e.isTrusted);
if (e.isTrusted) {
e.stopImmediatePropagation();
e.preventDefault();
e.target.dispatchEvent(new e.constructor(e.type, e));
}
}, true);
button.addEventListener('click', function(e) {
window.open('about:blank');
});
<button id="button">Test</button>
(In the snippet window.open won't work because of the iframe sandbox.)
Basically the idea is to add an event listener to the page that replaces any click event that is trusted with a copy of it that isn't trusted. However this doesn't work and the popup is still opened.
Is there any way to accomplish this?
Relevant specification here:
https://html.spec.whatwg.org/multipage/interaction.html#triggered-by-user-activation
This just answers the X part of this XY problem, because I see no real use for the Y.
If you wish to block all popups, then window.open = null; will already block all the ones made from this method, then you may also want to block the ones from anchor elements,
document.addEventListener('click', e => {
if(e.target.nodeName === 'A' && e.target.target === "_blank") {
e.preventDefault();
}
});
Now, you'll have to apply this in all the documents (i.e in iframes too) and you should be good.
But note that there are many legit reasons for pages to open popup windows, and disabling it will definitely break a lot of websites.
The pop-blocker's handling of event contexts seems to be complex. Even generating a setTimeout with an evaluated string does not break the context. In Firefox there is a period of one second after a click to perform a popup, later it is cosidered not to be triggered by a click.
However, I could get Firefox to block popups using setInterval. I did not test it yet in Chrome.
We overwrite the window.open method with a custom one:
window.open = (function()
{
const
openArgs = [],
fnOpen = window.open.bind(window)
;
setInterval( () => { for(let args; args = openArgs.pop(); fnOpen(args)); }, 100);
return function open(...args) { openArgs.push(args); }
})();
<button onclick="window.open('http://example.com');">button</button>
open
http://services.groupes.be/ibrunet/ibrunet.aspx?lg=NL
I am trying to simulate click events on DIV elements with class="x-grid-cell-inner"(with text Ibrunet, Signaletiek..)
First I inserted jQuery.
javascript:var s=document.createElement('script');s.setAttribute('src', 'http://code.jquery.com/jquery.js');document.getElementsByTagName('body')[0].appendChild(s);alert("loaded");void(s);
Then I tried this
var bedragenDivClass = "x-grid-cell-inner ";
var bedragenDivText = "Bedragen";
var divs = document.getElementsByClassName(bedragenDivClass);
for (var i = 0, len = divs.length; i < len; i++) {
if(divs[i].innerText.localeCompare(bedragenDivText) == 0){
alert("found");
};
};
And I've got referrence to this DIV but then I tried several different functions to trigger click event without any success
.trigger()
.triggerHandler()
.click()
When I open Chrome Dev Tools I can see several handler bounded to that DIV but I dont know how to trigger them
Unusual thing is that I could simulate click on input elements on right panel
var contractTypeInputId = "Cmb_Type_Contrat_Ibrunet_PLus-inputEl";
var contractTypeInput = document.getElementById(contractTypeInputId);
contractTypeInput.click();
Also I could click on elemenets of that input that show after script click it.
Since those DIV's don't have id attribute and I thought I need id to trigger the event I've gave them inside Dev Tools and I could retrieve it after but again no success with triggering onClick event.
The strangest thing is when I run something like this:
$("div").click();
I can see many DIV's beign clicked but those with that class I specified are not affected.
If I can trigger the event as simple as clicking on that DIV why I failed simulating it?
Using jquery
$(".-grid-cell-inner:contains(Ibrunet, Signaletiek)").click(function () {
alert(1);
});
$(".-grid-cell-inner:contains(Ibrunet, Signaletiek)").trigger("click");
I have a HTML page with multiple links. Is there any ability to catch the event of opening the link? Users can just click on link or can open it on the new window or tab by right click.
Links refers to external resources and I can not control of its content.
There's a few client-side options that I can think of, none of which ideal:
You could replace the link's href with a javascript: url that ran any code you wanted, and then navigated to the correct URL (location.href = "http://example.com"). This is nasty though, since it breaks if the user does right click -> new tab or has JavaScript disabled.
You can add an event listener to the click event of the <a>. But this would not get triggered on right-click -> open, or a user tabbing over and pressing enter.
You can use an onunload event handler on the document itself, but this won't tell you where the user is going, and wouldn't work if the user opened a new tab.
If you just want to track where your users are navigating to, then a server-side solution is the only foolproof way. You would have to send users to your own server, which would in turn send an HTTP 302 code to redirect the browser to the correct URL. This would, of course, require more infrastructure than you may want.
Yeah you can use javascript to do something like...
$('a').click(function(){
event.preventDefault();
$(this).alert("do something");
});
try
var links = document.getElementsByTagName('a');
for(var i = 0; i< links.length; i++) {
links[i].onmousedown = function(e) {
console.log(this.href, this)
if ((e.which && e.which == 3) || (e.button && e.button == 2)) { // not sure about IE
alert('right click');
}
}
}
Here is my code:
function Alert()
{
alert("Please click on OK to continue.")
}
window.onload = function()
{
var anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++) {
anchors[i].onclick = function() {return(false);};
}
};
the link is coded at this:
Link to Main Page
When I put this to test, the link does not work (as expected), but I don't get the alert box that's supposed to say "Please click on OK to continue." when the link is clicked. Can someone give me advice on how to fix this? Ultimately, once the page loads and the the link is clicked, it should show and alert that says "Please click on OK to continue." and nothing should happen. I know how to do this by changing the "href" attribute, but I need to know how to do it without changing that part.
You don't get the alert because your code changes all the "click" handlers, removing the ones coded in HTML. There's only one "onclick" property on the DOM nodes involved, so as soon as your script runs the handler that calls "Alert()" is replaced by the one that just returns false.
You can do it really easily with JQuery:
$('a').on('click',function(e){
e.preventDefault();
alert('your alert message');
});
you can test it in this fiddle: http://jsfiddle.net/kvCwW/
You need to add e.preventDefault() to your click handler:
anchors[i].addEventListener("click", function(e) {
e.preventDefault();
return false;
}, false);
I have a script that reloads an iframe whenever the browser screen is clicked (works perfectly). I was wondering how I would go about disabling the javascript from running on certain links?
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.
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;
}
}
There's two ways:
in the document.onclick, check the target element if it's a certain link, and then don't do anything. I.e. if (target.id == '...') { return; }
make sure that the click never arrives at the document.onclick. Because of how the DOM Event model works, if you cancel an onclick event at a low level (i.e. at a link itself) it will not bubble up and arrive at the 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!
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.
SIMPLE DEMO HERE