13

I haven't found anything that has worked. I've tried all of the following, to no avail:

  • added the attribute target="_blank" to the <a>
  • added target="someName" to the <a>
  • URL starts with '/'
  • URL starts with Meteor.absoluteUrl()
  • URL starts with Meteor.absoluteUrl() with the "http://" removed
  • URL as string literal, not returned from template helper
  • <a> inside {{#constant}} region in template
  • <a> not inside {{#constant}} region in template
  • <a> in the body outside of any template at all
  • <a> appended to the body in the browser console
  • window.open([url],[target]) with all the aforementioned combinations.

In all cases, the link opens in the same tab as where it was clicked, except for the URLs that didn't start with http://, which opened an about:blank page in a new tab.

Any idea what's causing this, or how to solve it?

zorlak
  • 1,414
  • 10
  • 15
  • Meteor was designed for single page web apps, which could explain this. Regardless, I'd say this behaviour is surprising – Swadq Dec 17 '12 at 07:51

4 Answers4

9

This seems like a bug. I think Meteor should ignore links with target="_blank". Maybe you could create an issue on the issue tracker

That said, I have successfully done this as a work around:

test.html

<template name="test">
  <a href="/new-window" target="_blank">Open new window</a>
</template>

test.js

Template.test.events({
  'click a[target=_blank]': function (event) {
    event.preventDefault();
    window.open(event.target.href, '_blank');
  }
});

Also, I have found that adding http:// works for external links E.g.

<a href="http://twitter.com" target="_blank">Open new window</a>

I'm not sure why these things didn't work for you. I have only tested them in Chrome, however, so maybe this is a browser issue.

Kyle Finley
  • 11,842
  • 6
  • 43
  • 64
  • That worked, thank you! I hadn't thought of doing it that way. Works in chrome and firefox so far, haven't tested in other browsers yet. – zorlak Dec 18 '12 at 05:45
  • While you may want an event handler for a button click, it's pointless for an `a href` element. Like any HTML page, these links are handled by the browser--you don't need to write any code. – ChatGPT Oct 24 '14 at 16:45
0

if using phonegap. answer is here phonegap open link in browser

Community
  • 1
  • 1
ChatGPT
  • 5,334
  • 12
  • 50
  • 69
  • I think you must be missing http from the url. Create a public repo which reproduces the problem and I'd be happy to take a look at it. – ChatGPT Apr 06 '15 at 01:35
  • Sadly, I'm not missing the http. Note that this happens when I package my meteor app for iOS. So, you're suggesting I create a proof-of-concept for you to compile an iOS app from? – MastaBaba Apr 06 '15 at 17:47
  • 1
    oh you didn't mention that. does this help http://stackoverflow.com/questions/17887348/phonegap-open-link-in-browser – ChatGPT Apr 07 '15 at 09:37
0

I'm for sure too late for the party, but for those who will end up here trying to find a solution to this problem (like me) I just want to make explicit something about the window.open([url],[target]) approach mentioned, for those who need a need window instead a new tab..

The third parameter for window.open is a list of comma separated specifications. The default value for the second parameter is _blank which will make the new window to be opened in a new tab.

window.open(event.target.href, "", "width=200, height=200"); should open the target URL in a new window (not a new tab) with the given dimensions.

Diego
  • 462
  • 10
  • 26
-1

create this global helper

Helpers.addHttp = function (url) {
  if (!/^(f|ht)tps?:\/\//i.test (url)) {
    url = "http://" + url;
  }
  return url;
}

using it in a template

<a href="{{addHttp url}}" target="_blank">some text</a>
ChatGPT
  • 5,334
  • 12
  • 50
  • 69
  • unfortunately not all websites with SSL (https/ftps) can be also be accessed without it (http). Additionally you're routing ftps links to http. – Ryan Taylor Dec 01 '16 at 20:27
  • No reasonable person would use addHttp for FTP links. You should create additional helpers like `addHttps` and `addFtp` – ChatGPT Dec 04 '16 at 04:52