close the window after opening the downloaded file - javascript

From my jsp, triggering a servlet request using window.open(url) that will return an ms-office file . After doing some action(open,save or close), the jsp should close automatically .
w = window.open(url, "_self");
$('#choiceSelection').html('<br><br><br><center><div id="reportContainer" class="reportContainer"><img src="loadingAnimation.gif"></div></center>');
//get the child window document loading status
statusX = w.document.readyState;
chk_timer = setTimeout("chk_popup_loaded()", 1000);
//alert(statusX);
//window.close();
}
function chk_popup_loaded() {
//alert("aaa");
statusX = w.document.readyState;
alert(statusX);
if (statusX != "complete") {
chk_timer = setTimeout("chk_popup_loaded()", 1000);
} else {
clearTimeout(chk_timer);
w.close();
}
if (statusX == "interactive") {
//alert("interactive");
//w.close();
window.close();
}
}
Is there any alternative to achieve this? The servlet will give response as ms office format file , it will download automatically to prompt us for save or open options. If I open that file current jsp file should close.
I've achieved this in IE8 using readystate but this feature is not available in IE11. How can I achieve this ?

Related

How to get Chrome to throw exception when changing url if it fails

I have a reference to a new window opened with js
var theNewTab="";
theNewTab = window.open(theURL, 'winRef');
then I change the url in the as the user clicks on another link in the parent window using
theNewTab.location.href = targetLink;
theNewTab.focus();
The problem i'm having with chrome that id doesn't throw exception if the the window doesn't exist anymore "closed" unlink FF & IE which im using to open the window again.
try {
theNewTab.location.href = targetLink;
theNewTab.focus();
}catch(err) {
theNewTab = window.open(theURL, 'winRef');
theNewTab.focus();
}
PS: I tried to use "window.open" every time but if the window already open, id does not reload the page or it does but it doesn't re-execute the script I have in document ready I think.
I'm not sure what you need.
<script type="text/javascript">
var theNewTab = null;
function openNewTab(theURL) {
if (theNewTab == null || theNewTab.closed == true) {
theNewTab = window.open(theURL);
} else {
theNewTab.location.href = theURL;
}
theNewTab.focus();
};
// use the function when you need it
$('a').click(function() {
openNewTab($(this).attr('href'));
});
</script>
Is this example helpful for you?

Load another html page without changing URL

I have some problem with page load on desktop browser and mobile browser. In this case I have 4 html page one is home.html, second is home-mobile.html, third is product.html and fourth is product-mobile.html. My problem is I don't know to switch the html page if opened in mobile browser. For the example is when I open www.example.com in desktop the page will call home.html but when I open www.example.com in mobile the page will call home-mobile.html and so with product page, when I open www.example.com/product in desktop it will call product.html but when I open www.example.com/product in mobile it will call product-mobile.html. The point is how could I open one link and it will detect opened in desktop browser or mobile browser and call different html page.
Which I have been done now but still not working is :
<script>
window.mobilecheck = function() {
var check = false;
if(window.innerWidth<768){
check=true;
}
return check;
}
if(window.mobilecheck()){
window.location.href="home-mobile.html";
}
else {
window.location.href="home.html";
}
</script>
But with that script the URL was changing and not be the same.
Please anyone know how to do this could help me. Thanks.
This script allows you to change the page content without redirecting the browser.
window.mobilecheck = function() {
var check = false;
if (window.innerWidth < 768) {
check = true;
}
return check;
}
if (window.mobilecheck()) {
$.ajax({
'type': 'POST',
'url': 'home_mobile.html',
'data': postData,
'success': function(response) {
$("html").html(response);
}
});
}
modify the code as you need.
If your device mobile it will automatically redirect on mobile page. Simple use this code. No need for else condition. just check mobile device.
if ($(window).width() < 767) {
function getMobileOperatingSystem() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
"Android"
if (/windows phone/i.test(userAgent)) {
return "Windows Phone";
}
if (/android/i.test(userAgent)) {
return "Android";
}
if (/iPhone|iPod/.test(userAgent) && !window.MSStream) {
return "iOS";
}
return "unknown";
}
if(getMobileOperatingSystem()){
window.location="\home-mobile.html";
}
}
Let's say your page is home.html.
And when the page is loaded, you can run a script to detect client type( desktop or mobile), then send an Ajax call to corresponding page to get the content you want.
Then when the Ajax call returned, you can simply replace current content with the returned value.
In this way, your URL will not change at all, and the content can change according to current client type.
home.html:
<html>
<head>
<script language="javascript">
function loadContent(){
var clientType
//your code to get client type here
//Below block to get Ajax client object according to your browser type
var XHTTP;
try
{
//For morden versions Internet Explorer
XHTTP = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (ex)
{
try
{
try
{
//For old versions Internet Explorer
XHTTP=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(exxx)
{
XHTTP = new XMLHttpRequest("Msxml2.XMLHTTP");
}
}
catch(exx)
{
try
{
//For browsers other than Internet Explorer
XHTTP= new XMLHttpRequest();
}
catch(eexx)
{
//This means ActiveX is not enabled.
//To enabled go to your browser settings and enabled the ActiveX.
alert("Not Supported, It might be previous version browser");
}
}
}
if(clientType=="mobile"){
XHTTP.open("GET","/home_mobile.html");
}else{
XHTTP.open("GET","/home_desktop.html")
}
XHTTP.onreadystatechange= function()
{
if (XHTTP.readyState==4)
{
var result=XHTTP.responseText.toString();
document.getElementById("content").innerHTML=result;
}
}
//This finlly send the request.
XHTTP.send(null);
}
</script>
</head>
<body onload="javascript:{loadContent();}">
<div id="content"></div>
</body>
</html>
This script will change the html content of a current page with requested page html without changing url. (AJAX)
var xhr = new XMLHttpRequest(); // Create XMLHttpRequest object
xhr.onload = function() { // When response has loaded
// The following conditional check will not work locally - only on a server
// if(xhr.status === 200) { // If server status was ok
document.getElementsByTagName("html")[0].innerHTML= xhr.responseText; // Update
//}
};
xhr.open('GET', 'html u want to replace ', true); // Prepare the request
xhr.send(null); // Send the request
});

javascript - When a url is opened in one window of IE then from other window of IE on button click the already opened should come to focus

CASE 1: I have the above requirement in IE; that if i already open an instance of a URL from VS or a deployed URL in one window of IE (for example already opened url: www.onbuttonclick.com)
CASE 2: Now i have opened an application where i have written a button click event that on click i need to open a particular url (Working fine if www.onbuttonclick.com is opened from this button click event)
Requirement: i have to write some code in www.onbuttonclick.com that when it is getting loaded it needs to check if the url is already opened in any of the tab or other windows of IE ??????? please suggest a suitable solution....
I have tried the below approach and it is working fine if www.onbuttonclick.com is not already opened........
var memberURL = document.URL;
var newWindowCopy = document.URL;
var winBrowser = window.clientInformation.appName;
var stringText = window.clientInformation.userAgent.toString();
var localVariable = "IamAliveInLocalStorage";
if (winBrowser == "Microsoft Internet Explorer" || stringText.indexOf('MSIE') >= 0) {
if (localStorage.getItem("MyStorage") == localVariable) {
open(memberURL, newWindowCopy).close();
}
else {
localStorage.setItem("MyStorage", localVariable);
window.onbeforeunload = function () {
localStorage.removeItem("MyStorage");
localStorage.clear();
};
}
}
else { }

Open print dialog receiving a pdf file from controller C#

I have a controller that is returning a pdf file, this is temporary save in a local folder. It is possible to open a print dialog directly with out open the pdf file using javascript??
public ActionResult LoadDownloadAndPrint(string Download)
{ return File(System.IO.File.ReadAllBytes(target);)
The file is saved in C:\Windows\Temp\3ac416b7-7120-4169-bc4d-61e105ec197c\output.pdf
I've have tried to used embed tag like in this thread Print PDF directly from JavaScript but did not work I guess because is a local place where the file is stored.
After a lot of researching finally get it using iframe. This code was very helpful http://www.sitepoint.com/load-pdf-iframe-call-print/
I have an empty hidden iframe in my view.
This is working in Chrome, FF, but not in IE
Search.openPrintDialog = function () {
var printJobIds = $("input:checkbox[name='Print']:checked").map(function () { return this.value; }).get().join(',');
if (printJobIds.length > 0)
{
JSUtil.BlockUI();
print('/' + JSUtil.GetWebSiteName() + '/Search/LoadDownloadAndPrint?Print=' + printJobIds)
}
return false;
}
function print(url) {
var _this = this,
iframeId = 'iframeprint',
$iframe = $('iframe#iframeprint');
$iframe.attr('src', url);
$iframe.load(function () {
_this.callPrint(iframeId);
});
}
//initiates print once content has been loaded into iframe
function callPrint(iframeId) {
var PDF = document.getElementById(iframeId);
PDF.focus();
PDF.contentWindow.print();
JSUtil.UnBlockUI();
}

jquery/jscript Prevent open multiple popups

I open a popup with the click event of a hyperlink... The popup contains records from a server.
The problem is that when I click rapidly, there are multiple popups at once.
There is a way to prevent this? in which can open a single popup
My code:
$('.wrapper_form a.add').click(function(e)
{
e.preventDefault();
if(typeof(currentPopup) == 'undefined' || currentPopup.closed)
{
url = 'server_page.aspx';
currentPopup = window.open(url,'server','height=500,width=800');
if (window.focus) {currentPopup.focus()}
}
else
{
currentPopup.focus();
}
});
Here is one approach. Not the best solution but it should work. What this code will do is protect against clicking the link a bunch of times and have it open a new instance for each click. This code will not allow the window to be opened more than once in a 1/2 interval, of course you can change the timing.
var hopefullyThisIsNotInGlobalScope = false;
$('.wrapper_form a.add').click(function(e)
{
if (hopefullyThisIsNotInGlobalScope)
{
return false;
}
hopefullyThisIsNotInGlobalScope = true;
setTimeout(function () { hopefullyThisIsNotInGlobalScope = false; }, 500);
e.preventDefault();
if(typeof(currentPopup) == 'undefined' || currentPopup.closed)
{
url = 'server_page.aspx';
currentPopup = window.open(url,'server','height=500,width=800');
if (window.focus) {currentPopup.focus()}
}
else
{
currentPopup.focus();
}
});
Assuming the popup is on the same domain as the window launching it you might be able to replace hopefullyThisIsNotInGlobalScope variable with a global var attached to the window. You can then set that variable when the popup launches and alter it using the browser unload event

Categories