1

I have the following code in JS:

$('#external-apply-job a').click(function(e) {
            var button = $(this);
            var url = $(this).data("system-url");

            loadPreloader();

            $.ajax({
                url: url,
                type: 'GET',
                async: false,
                data: {mode: 'json'},
                dataType: 'json',
            })
            .done(function(data) {

                if (data.type !== 'danger')
                { $(button).html(data.message); $(button).addClass('clicked'); $(button).trigger('click');} 
            })
            .always(function() {
                unloadPreloader();
            });
    });

And this is the link :

<a target="_blank" href="generated url"  title="some name">Sistem</a>

I know that using

$.ajax({
                    url: url,
                    type: 'GET',
                    async: false,
                    data: {mode: 'json'},
                    dataType: 'json',
                })

the click should be trusted event, but what is wrong is this code PS: If I use target="_self" all is ok but I need to open a new window. THX.

Neculai Andrei
  • 223
  • 5
  • 17
  • 1
    See [Trigger click on input=file on asynchronous ajax done()](http://stackoverflow.com/questions/29728705/trigger-click-on-input-file-on-asynchronous-ajax-done) – guest271314 May 18 '16 at 07:03
  • Should it open in a new tab/window? If not, why not remove the attribute? Side note: Sync call is being deprecated by all major browsers. Try async: true – Raphioly-San May 18 '16 at 07:04
  • @Raphioly-San Thx for the sugest, I used `async: true` and all is working fine now. – Neculai Andrei May 18 '16 at 07:38

2 Answers2

1

When the user clicks on your link, your ajax request should work as expected. The problem occurs after changing the link's code to the result of the ajax request and then programmatically call the the event handler via trigger(). An ajax request triggered by the program will not work due to security restrictions, at least if they open a new window / tab.

If you only want to open a new window or tab with an url, you could simply use window.open().

JSchirrmacher
  • 3,243
  • 2
  • 19
  • 27
0

@Raphioly-San Thx for the sugest, I used async: true .

Neculai Andrei
  • 223
  • 5
  • 17