I have a cshtml page that uses Kendo UI ASP.NET MVC that does the following:
The view contains one or more grid widgets (the number depends on how many result types were requested) and each grid is set up to export Excel.
The problem is that when window.open() is used to open a new tab, the browser history state is null and the window.location.href is empty with the browser location showing "about:blank". For Chrome and Firefox, this does not cause any issues when exporting excel for the grid. However, when using Edge with this situation, when the Export to Excel grid toolbar button is clicked and the onExportExcel event is fired, the browser Open or Save dialog prompt is displayed but the active tab (the one that contained the grid) closes. This behaviour is very undesireable. The ProxyUrl grid excel option does not fire since Edge supports javascript file saving.
A workaround for this situation is to forcibly set the new tab window object location href by using the window.history.pushUpdate function. A code snippet is included below.
let dataModel = {Id = 123571113, Name="jason bourne"}; let jsonModel = JSON.stringify(dataModel); let curDate = new Date(); let targetUrl = '@Url.Action("Reports", "Report", new { @area = "Reports" })'; let targetWindowName = "something meaningful" + " " + curDate.toISOString();//add datetime stamp to avoid issue where you cannot open a window with the same name as the current window. let html = "some html content to provide a temporary message to your audience"; let targetWindow = window.open(targetUrl, targetWindowName); if (targetWindow !== null && targetWindow !== undefined) { targetWindow.document.write(html); targetWindow.document.close(); // to finish loading the page targetWindow.document.title = targetWindowName; //attempt to forcibly update the URL in the history and target window location to fix problem with grid export to excel on Edge browser let targetWindowHistoryHref = window.location.href; if (oModel.BuildingDesigns !== null && oModel.BuildingDesigns !== undefined && oModel.BuildingDesigns.length > 0) { targetWindowHistoryHref += "?oBuildingDesignId=" + oModel.BuildingDesigns[0].ObfuscatedBuildingDesignId + "&BuildingDesignName=" + oModel.BuildingDesigns[0].BuildingDesignName; } else { targetWindowHistoryHref += "?" + oModel.Target; } targetWindow.history.pushState(null, null, targetWindowHistoryHref); } $.when( $.ajax({ type: "POST", dataType: "html", // this is the data type expected to be returned from the controller method contentType: "application/json", // this is the content type expected by the controller method url: targetUrl, data: jsonModel, beforeSend: function() { console.log(".... submitting report request"); athena.loader.loading("submitting report request"); }, success: function(response) { athena.loader.stopLoading(); if (debugLevel > 2) { console.log(".... response = " + response + " : ", response); } //attempt to populate the target browser tab with the response try { console.log(".... attempting to open a browser tab and populate it with the HTML response object"); console.log(".... targetWindow = " + targetWindow); //will trigger popup blockers :: targetWindow = window.open("", oModel.Target); if (targetWindow !== null) { if (response === null || response === undefined) { targetWindow.document.body.innerHTML = ''; } else { //completely replace the existing document (not just the innerhtml) targetWindow.document.open(); targetWindow.document.write(response); targetWindow.document.close(); targetWindow.document.title = targetWindowName; } } } catch (ex) { // do nothing, just catch when the open fails. console.log("error: " + ex.message); } }, error: function(jqXhr, textStatus, errorThrown) { console.log('.... error :: ajax status = ' + textStatus + ' :: errorThrown = ' + errorThrown); console.log('....-- jqXhr.responseText :: \n' + jqXhr.responseText); } }) ).done( function() { console.log(".... report request has completed"); targetWindow.focus(); } );