Click link and call javascript and then "Open in new tab" - javascript

I have a link in my page.
<a onclick="Click()">Click me</a>
When I click this link, fires onclick event that call Click function. In this function, I have some javascript code and then, I want to redirect to another page.
function Click() {
//some code
window.location = url;
}
When user right click on link and select "Open in new tab", new tab opens, and url is: "about:blank".
How to I correct this?
Thanks.

The key is not to replace anchor functionality with Javascript. Anchors are designed to take you to a new URL. If you have an anchor, use it as an anchor (eg, set a href on it). Don't bind a click event that uses window.location to redirect to a new url.
Your current anchor has no href. So when you try and open it in a new tab, there's no surprise that nothing loads. You're literally opening nothing in the new tab.
If you really need to set the URL dynamically, then change the href of the element using javascript. For example:
document.getElementById('my-element').href = new_href;
It goes without saying that this needs to be done before the anchor is clicked (not on click). For example, on window.load, or the completion of the function that generates the dynamic URL.

Use window.open function and pass _blank in second argument function
function Click() {
//some code
window.open(url,'_blank');
}

you should correct your anchor tag to be something like this:
link text
this way when user click on the link it'll open in new tab and you'll execute the Click function.

Related

How to activate an <a> element on a web page with 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();

javascript cancel href link when clicked <a href>

If i have multiple links on a website, is there a way that if they are clicked they can be transferred to another website? All the links can be the same, but when either one is clicked they transfer over to the same page.
I'm trying to find an event that cancels the bubble when the event is clicked but can't find one.
Say for example I have
google
when that link is clicked then it will cancel the google.com transfer and open yahoo.ca
Using jQuery you could bind a click handler that prevents the default, and instead redirects the page, like so.
$('a').click(function(ev) {
ev.preventDefault();
window.location = "http://www.example.com"
});
If you mean you can have any href in link and you click any of links in page, then you need preventDefault:
$('a').click(function(e){
e.preventDefault();
window.location = 'http://yahoo.ca';
});

Can I use href like an onclick?

I just can not figure a way to use location.href to act like you're clicking on a link.
What I am trying to do is get the variables included, for example,
The regular link is like this and works fine:
Click Me
I am trying to call a function using href (or some other method) to open the image with the other parameters just as if you were to click on the link.
I hope I am explaining this correctly.
Thanks for any help.
-UPDATE-
I finally found my solution here Displaying the Popup box generated by Greybox on page load(onLoad)
Thank you all for your suggestions.
Add an onclick event to your link. Using JQuery, you could ofcourse bind the click event to the links, and use .preventDefault() to block the anchor tag workin. Much cleaner code also.
$(document).ready(function(){
$('a').bind('click', function(e){
e.preventDefault();
//do whatever you wish to do
});
});
Answer: Yes.
Click Me
location.href or window.location.href refers to the browsers current address. If you change the value of location.href the browser will use that value as it's address. So if you want to navigate your browser to a link programmatically, you just have to change its value.
If you just want a clickhandler for your link don't use onclick or href, that's just bad practice, use a sepperate script that does this unobtrusively like so:
var button = document.getElementById('button');
button.onclick = function(){
do_something();
}
<a onclick="yourJSFunction();return false" href="images/someImage.jpg" title="My Image" rel="gb_page_center[249, 266]">Click Me</a>
You just need to add an onclick attribute to your anchor tag.
Make sure to return false or else the anchor tag will go to the URL like normal.

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/

Categories