0

I have a SharePoint .aspx page, a webpart which contains a link that opens a page, and I can't change the webpart. How can I add JavaScript that opens a page in a new window?

This code helps, but I need to locate the webpart link, and handle a click on the link by opening a new page:

<a href="http://URL" target="_blank">LINK</a>
Sean Mickey
  • 7,618
  • 2
  • 32
  • 58
  • [easy enough to find out](https://www.google.co.uk/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=js%20open%20new%20window%20from%20link) – GillesC Dec 02 '16 at 22:19
  • Did you mean to include an actual URL at "LINK"? – Michael Armes Dec 02 '16 at 22:19
  • 2
    Possible duplicate of [Open a URL in a new tab (and not a new window) using JavaScript](http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window-using-javascript) – GillesC Dec 02 '16 at 22:19
  • 1
    OR in JS: window.open(url,"_blank"); – hack3rfx Dec 02 '16 at 22:21

1 Answers1

0

In your JavaScript, you need to first obtain a reference to the link's <a> element. You can do this through a number of different methods, such as document.querySelector() or document.getElementById().

Once you have a reference to the element, you can add the target="_blank" attribute by calling element.setAttribute("target","_blank") (where element is the variable containing the reference to the <a> element.

The exact code needed depends on the HTML in the web part that contains the link, since that will determine how you can get a reference to the element.

Thriggle
  • 7,009
  • 2
  • 26
  • 37