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)?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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” (e.g. 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.
Also, see this “Key event properties” table for more information. Seems jQuery sets the metaKey property to
event.ctrlKeyif its undefined (search for “metaKey” in jQuery’s source).