I currently made a link that I attach the "click" event to ajax some content. How do I make it so that the user can open it in a new window or tab if they want (either through context-menu or shortcuts)?
If you provide a valid href-attribute, users should be able to open the link in a new tab by selecting "Open in new tab" from the context menu .
To detect if a user clicked on the link while holding down any "special key" (eg alt, shift, "meta", this last one is the one you want I guess), use the click event's properties provided by jQuery: see this example on jsfiddle and the respective section in the jQuery docs . Hope it helps to catch the CMD-clicks commonly used to open a link in a new tab/window.
$("a").on("click",function(e){
if(e.metaKey) {
// the user probably wants to open the link in a new tab
} else {
// simple click, so do your AJAX call here
}
});
Also, see this "Key event properties" table for more information. Seems jQuery sets the metaKey property to event.ctrlKey
if its undefined (search for "metaKey" in jQuery's source ).
add attribute target="_blank" in hyperlink. So, on click of the url, new window will be used.
<a href="http://www.google.com" target="_blank">click here </a>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.