Example of what I want to do: On Facebook, clicking a link to open it in the current tab will trigger some JavaScript rather than opening the link. However, opening it in a new tab (either by right-clicking or by holding Ctrl/Cmd) will open the link without calling any JavaScript.
I'd like to do the same with my links (have link behaviour dependent on the target). I have the onclick event handler return false; to avoid opening the link; but this causes Ctrl+clicking to fail to open the link. How do I achieve this?
EDIT: I do not want links to forcefully open in new windows. IF a link is opened in a new window, I want it to follow the href as usual. However, IF the link is opened in the current window, rather than following the link in the current window, I want to have some JavaScript run.
Facebook does this to open an image in a popup "theatre" if you open it in the current window, and open it in a full page if you open it in a new window.
Note that capturing the click event on links and using preventDefault() or return false causes Cmd+Click (open in new tab) to fail. In any case, this should work regardless of how the link is opened - click, Enter key, etc.
EDIT 2: It seems that hashchange / HTML5 pushState is the correct way to go about this
You can set the href of the anchor pointing to correct url. This way right click and open in a new tab or window will work fine.
Use jQuery to bind the click event handler to the anchor like this.
$('a').click(function(e){
//Do whatever javascript operation you want.
if(!e.ctrlKey){
e.preventDefault();//Stop the page to navigate to the url set in href
}
});
Demo
Related
Below link will be disappear after 1 click only in firefox, how to fix this?
Open page in new window
Fiddle Link
The link does not disappear, the problem is that firefox still links to the page in the attribute href in this case in the jsfiddle, it's linking to google.com, but google doesn't allow to embed google in an iframe, so you get a blank page in jsfiddle.
if you use javascript:void(0) inside your href the link will do absolutely nothing, if you use # it will link to a non existing anchor on your page, this is not a problem, but you see it in the address bar of your browser.
You can try this instead:
Open page in new window
If you just want to open the link in a new tab instead you can use target for this, some old browsers will still open this in a new window:
Open page in new tab
Try this:
Open page in new window
I have a web application where I have four/five icons in the default page. On click of these icons i am redirecting to some URL in new tab of already opened chromium browser instance using javascript. Below is the javascript code snippet that I am using to launch the URL in new tab of already opened chromium browser instance. Here I am manually clicking the icon
window.open(myURL, '_blank');
But the issue is when I am trying to open the same URL using the same code and programatically triggering the same code to open the URL in new tab, it opes up as a Pop Up but not in a tab. Below is how I am trying to pragmatically open the URL. I am calling the click event of the Icon in the default page using some script rather than manually clicking the icon.
$('#myIconId').click();
If somebody could please help me out. TIA
give a try to use
content of the anchor
Although You shouldn't force user to open new pages or new tab without showing them a hint on what is going to happen before they click on the link.
Above code may or may not work on all browser, please test before making live.
UPDATE
Ref : Programmatically open new pages on Tabs
Now I have a link
link
However, this always open up a new tab. I want the following effect
If the user already has a tab with the same URL, reuse that tab, and refresh if possible
Otherwise, open a new tab
How can I achieve this using JavaScript?
It's OK if there're only some browser-specific methods, so users of browsers without corresponding support will "fallback" to the always-new-tab way.
You can set specific window's name, in order to open reuse the tab. The problem is, as far as the href will be the same, it won't be reloaded. So you can't obtain the refresh part easily.
So, for instance, you can have:
link
link
In JS, you can actually obtain the same, using window.open. You could also use the url as target, so that you don't need to specify manually:
link
link
You could also generalize, and add a click listener to the document, in order to open some links in this way. Something like:
<div id="container">
link
link
</div>
<script>
document.getElementById("container").onclick = function(evt){
if (evt.target.tagName === "A")
window.open(evt.target.href, evt.target.href);
return false;
}
</script>
If the page are on the same domain, at this point you could probably trying to do an empiric refresh of the page as well.
setting up target="child" would refresh the current tab if already opened
ABcd
Defg
I search a lot but could not figured it out.
Is it possible to open a new window with tab enabled using window.open() method?.
The way chrome context menu "open link in new window" works.
The browser decides on its own how links will open. Sometimes a user can configure how links are to be opened, but the web page cannot do this.
using an anchor tag you can set the target attribute to have some control over where the link opens. Other than that, this behavior is up to the browser and user and cannot be controlled by javascript.
Use this code
window.open("http:.//www.google.com","DemoName",'scrollbars=1,height=650,width=1050');
I have same ancors/hyperlink in my html file. These point to a new website outside my site.
So I want that when the user clicks the link it should open a new tab or window. My website page should not be closed.
How can it be done?
open in tabs: there is nothing programatically that you can do to accomplish that, the only thing I'm thinking of is set the browser to open new links in tabs instead of new window...
to open in a new window all you need is to place a target in your anchors
click here
and, by the way there are more options to the target
target="_blank"
opens in a new Blank window
target="_parent"
opens in a Parent window (used when dealing with iframes and you want to open the link in other frame)
target="_self"
opens in it's own/self window (used when dealing with iframes and you want to open the link in the current frame)
target="_top"
opens in the top of all frames (it will open on top of all frames in the page, like a no-frame page will be display)
Click me to open in a new tab/window
Load the linked document into a new
blank window. This window is not
named.
If there is no existing window or
frame with the same name as specified
in the target, a new window is opened
with a name equal to the value of the
target.
Its the setting in a browser that determines whether to open the page in a new tab or in a new window.
Just add target="_blank" to the <a> tag
Click here
The easiest way is to use the target attribute in your anchors and set it to _blank:
Worse Than Failure
This is a dupe of opening links in the same window or in a new (tab) and https://stackoverflow.com/questions/118567/is-there-ever-a-good-reason-to-force-opening-a-new-browser-window. Basically - no. And there shouldn't be a way - you shouldn't impose your surfing preferences on unsuspecting others.