Search code examples
javascriptangularjsangular-ui-routerpopupwindowui-sref

Popup window link launching in window as well as in the browser tab


I'm working on a website built using Laravel and AngularJS. I want a certain link to open in a popup window. The javascript code is

function popitup(url) {
    newwindow=window.open(url,'test','height=400,width=550');
    if (window.focus) {newwindow.focus()}
    return false;
    }

and the link is

<a ui-sref="test({id:test.id})" class="btn btn-primary fright" onclick="return popitup(this.href)" oncontextmenu="return false;">Resume</a>

when I click on the button the popup works fine but the link also opens up in the tab where I clicked it, but I don't want it to. Is there a way to do it?


Solution

  • I would guess ui-sref will also bind a click-event and that event will trigger before yours.

    You could skip the ui-sref and put the url in a data-attribute or inside the onclick-attribute. You could get the url using $state.href() instead. Then the problem may disappear.

    Edit

    You could bind it to a scope function and skip onclick all toghether. Something like this:

    In the Controller (Also make sure you include $state in the controller first):

    $scope.popitup = function(stateName, options) {
     var url = $state.href(stateName, options);
     newwindow=window.open(url,'test','height=400,width=550');
     if (window.focus) {newwindow.focus()}
    }
    

    And in the HTML you invke the popuitup function with the state name and its parameters:

    <a ng-click="popitup('test', {id:test.id})" 
       class="btn btn-primary fright" oncontextmenu="return false;">Resume</a>
    

    Also see documentation for state.href.