How to activate an <a> element on a web page with javascript? - javascript

I want to click on this element.
ini
There are other elements, just like that where only the href changes. I can correctly find the element I want. But cannot click it...
I tried the usual click() but didn't work:
document.getElementsByClassName("btn")[1].click()
Any help?
I tried this, it opens the same window, but I want to open in a new tab. I also put the internet option to open tabs with popups:
b=document.getElementsByClassName("btn")[1].href;
window.location.assign(b, '_blank');
How to open in new tab? I tried window.open() but never got to make it work:
window.open(b, '_blank');

You can find the answer here How can I trigger a JavaScript event click. If you execute the .click() function, you trigger all the listener that are bind on the 'click' ( .onClick ).
Try to listen for the click event and then execute the .click() function

I'm not really sure i understand the question all the way. But when you actually click a link all other JavaScripts stops loading and you get to that webpage. The window.open is right. But you have to prevent the Url to actually redirect you to the website. You can do this by using preventDefault on the eventobject that fires on your click. Hope this helps
let myLink = document.getElementsByClassName("btn")[1];
myLink.addEventListener('click', function newWindow(event) {
event.preventDefault();
window.open(this.href, '_blank')
})
myLink.click();

Related

How to unbind an arbitrary listener in jquery/javascript?

If there is some library (blackbox) that is putting a click listener on an a tag, that does something like do an alert in this case, it is also blocking the actual href navigation from working.
Is there a way in javascript/jquery to unbind that specific click listener and make the href clicking to work?
Note: I don't have the reference to the actual click function.
Thanks
What about
$(document).on('click','a',function(){$(this).preventDefault();console.log('a click'); return false;});
as a way to "disable" all present and yet to be generated <a> tags?

Detect link click event in chat window

I need to capture the event that occurs when a user clicks a link on my chat application. I am using IE11.
Is there a way to capture the user clicking the link, when such a link could be dynamically added to the chat box (i.e. user sends "www.google.com" message) at any given time?
I have been using onbeforeunload by the way and while this detects the browser close event it will not detect the link click event, I am not sure why, so I was thinking that a jquery solution that checks the links on the page for an onclick could solve my problem...
Thanks,
dearg
Yes, you can use event delegation like:
$("#chatWindow).on('click', 'a', function () {
//do something
});
You could do it with a function like this:
$('a').on('click', function(){
//track clicked link here
return true; //to allow following the link this is the default behavior no need to add
return false; //prevent default behavior
});

stopping the right click and go to link function for a tag

I have a tag like below. The question I have is how do I make this <a/> tag not behave like a link when user right clicks it. Since on a regular click the onclick event will fire and return false I am good with the regular click on the link the issue comes when a user right clicks the mouse and then gets the option like open in new tab or open in new window I have to prevent this from happening. I found out I can use javascript:void(0) in the href to do that but for some reason I cannot change the href as it is used for some other stuff. Is there any even or something that I can use.
<A title="Test1" onclick="javascript:search1('search'); return false;"href="team">search</A>
Thanks
as often, there's no universal solution, every browser do it its way. HTML 5 says form.oncontextmenu event handler should be supported. So this
<script>
document.oncontextmenu=function("alert('dont play with sources');return false");
</script>
should work if you use HTML 5.
you can also remove the javascript word, onclick already waits for js code (as oncontextmenu does).
<a onclick="search1('....

How do I simulate clicking a link that opens in a new tab/window using jQuery/Javascript?

I have a link that opens in a new tab with _blank:
link
I'd like to be able to 'click' on this from Javascript. I know I could do document.location=... but the problem here is the new tab part. Is this possible?
You can usually open a new window or tab using window.open. So instead of setting location, just call window.open and pass only the URL, nothing else.
$("#your_a").click(function(e) {
e.preventDefault();
window.open($(this).attr("href"), this.target);
}
You could use any name for target, preventDefault() if you want to override the link actions completely
If you really want to simulate click, you can give a id to the anchor tag and call jQuery click() on that element.
<a id="mylink" href="new_page.html" target="_blank">link</a>
and in script
$("#mylink").click()
to "simulate" the click you trigger the click event:
HTML:
click me
jQuery:
$("#mylink").trigger("click");
-or-
$("#mylink").click();
Using 'trigger' allows you to pass in extra parameters if you bind a function to the click event. See the jQuery 'trigger' documentation for more info: http://api.jquery.com/trigger/

In IE the onbeforeunload event is fired for links that don't unload the page

I am writing a plugin to a CMS (umbraco) and I wish to attach a warning dialog to various actions on the page, one such action is clicking a link (JavaScript links), in most browsers the following code works well:
$(".propertypane").delegate("a, a div", "click", function () { window.onbeforeunload = confirmNavigateAway; });
The following is an issue in IE because IE appears to trigger onbeforeunload event when any link is clicked, even though the link is not navigating away.
I've set up an example here:
http://jsfiddle.net/DETTG/8/
Note: I do not have control over the ajax controls within the propertypane, they're written by third parties.
Maybe this page will help you?
If you remove "href" then it will work. But then you would need to style it as a link element and add the attribute onclick if you want to execute a function. Here is the updated version: http://jsfiddle.net/DETTG/34/
<a onclick="alert('do some ajax');" style="color:blue; text-decoration:underline; cursor:pointer">javascript</a>

Categories