Hi,

within the navigation menu i have a link to an external website and wonder if there is a way to change the target of a link?
< href=... target="new_window">...

Any ideas?

Comments

vm’s picture

investigate the ext_link.module

liegebuggy’s picture

thx, it works with the default theme.
but when using it within my own theme (out of the admin area) it does not show an icon and does not add an target :-(

how to repair this?

vm’s picture

I imagine it doesn't work in your theme because you may not be printing the <$scripts> or <$closure> variables in your page.tpl.php file? compare with a core theme and ensure you are using all variables required.

ranj.sarraj’s picture

go to theme folder and add the following javascript to page.tpl.php file before tag

<script> 
window.onload = function() {
   if (!document.getElementsByTagName) return false;
   var links = document.getElementsByTagName("a");
   for (var i=0; i<links.length; i++) {
      if (links[i].getAttribute("href").match('#newpagemenu') == "#newpagemenu") {
         links[i].onclick = function() {
            return !window.open(this.href);
         }
      }
   }
}
</script>

them go the menu and add #newpagemenu to each item that you want to be open in new target, I had test it and it should work

karlkablisk2’s picture

You don't know how long I was looking for a solution to this. Thanks it works for me.

The only thing is, what if I want to open to a target iframe instead of a blank new window?

Thanks!

ranj.sarraj’s picture

check this link, it should help you to modify the window.open attributes
http://msdn.microsoft.com/en-us/library/ms536651(VS.85).aspx

Andrew_Scott’s picture

This script works like a charm for the menus, as well as for another problem I had with a javascript module generating links that I wanted to open in a new page but would not carry over the target=_blank. Thanks for this!

michellezeedru’s picture

blade_rsw2003, thanks for this solution! I loved the approach, but kept getting a javascript error (this.href is null). Not knowing any javascript myself, I had a colleague take a look for me, who rewrote as a jquery function. Below is the script we ended up with. Note that we are using a class of "popmeup" instead (using the module Menu Attributes to set the class), and we added some extra attributes for setting the window size (note that I don't normally condone that kind of thing, but in this case, it was the appropriate solution).

<script>
$(document).ready(function () {
	$(".popmeup").click(function (evt) {
		evt.preventDefault();
		window.open(this.href,'listener',"height=200,width=400");
	});
});
</script>
travisimo’s picture

<script>
window.onload = function() {
   if (!document.getElementsByTagName) return false;
   var links = document.getElementsByTagName("a");
   for (var i=0; i<links.length; i++) {
      if (links[i].getAttribute("href").match('http://www.') == "http://www.") {
         links[i].onclick = function() {
            return !window.open(this.href);
         }
      }
   }
}
</script>

This variation of your code above will negate the need for a #tag and will simply detect links that start with http://www. to be dictated to open in a new window. I prefer this because the #tag transfers to the new window in the URL bar which is unsightly.

Thanks for the code though, works awesome!

ranj.sarraj’s picture

;-)