Search code examples
aframe

A-Frame : How to open a dynamically created a-link in a _blank page


this is specific to A-Frame.

I'm creating an a-link from javascript code:

var alinkEl = document.createElement('a-link');
alinkEl.setAttribute('href', 'http://www.facebook.com/share.php?u=https://.../' + folder + fileName);

This does not work:

alinkEl.setAttribute('target', '_blank');

Any hint?


Solution

  • The <a-link>s are intended for link traversal as far as i know, not for opening new tabs. The component does not have a target attribute in the schema. Furthermore, the navigation is implemented like this:

    navigate: function () {
      window.location = this.data.href;
    }
    

    So You only change the location of the given window.


    To open a new tab I would would do the portal, and the link myself:
    If You want a portal use the portal shader, and set the pano attribute:

    <a-circle position="0 3.5 -2"
    material="shader:portal;pano:_URL_TO_PORTAL_IMAGE"
    ></a-circle>
    

    If You want it to open a new tab, make Your own component:

    AFRAME.registerComponent('foo', {
      init: function() {
        this.el.addEventListener('click', (e) => {
          window.open('https://ebay.com');
        })
      }
    })
    

    And attach it to Your portal:

    <a-circle position="0 3.5 -2" material="shader:portal;pano:_URL_TO_PANO" 
    foo></a-circle>
    

    Working fiddle here.