0
votes

I am trying to open a new tab when a certain menu link is clicked in Drupal. One way I thought about doing is embedding a Javascript code within the content of that page. However, that does not seem to work.

Specifically, I place this code inside the HTML content of that page:

<script>

$(document).ready(function() {
  var popup  = window.open("about:blank", "_blank"); // the about:blank is to please Chrome, and _blank to please Firefox
popup.location = 'http://google.com';
});

</script>

Is there another way of achieving this? To clarify, I still want the menu link to navigate Drupal to whatever page is assigned to it. However, I also need a new tab to open upon the click.

1
You sure the browser's popup blockers aren't blocking this? Any errors in the console? - Joseph
I am pretty sure. Actually, if I add this code into the content, it does not even show in the Page Source Code. It seems as if the Drupal is filtering it out from displaying, despite its being inside content. - MadPhysicist
Notice that the window.open takes first argument as a url and so you need to do something like this: window.open("http://somewebsite.com", "_blank") - Bubble Hacker
I was able to get it working, but it comes as a pop-up (lame) and gets blocked by the browser. There has to be a way to do this since we can click links and have them open in new tabs. One would just need to get Drupal to emulate that behavior. - MadPhysicist

1 Answers

0
votes

A much easier method for what you are trying to do would be to install a module, such as Menu Target. In the menu item you want to be opened in a new tab you just need to click on the 'Open this link in a new window' checkbox.

There are other modules like Menu attributes, but I think the first one would work better for what you are trying to accomplish.