I am trying to create an HTML file that when run, will open a URL in a new tab (the URL is pointing to a file on a local server, so when the HTML is run it in fact prompts the user to open or save the file), and then after x seconds, closes itself without the prompt asking the user if they wish to close the tab.
Only Internet Explorer can be used, as my company does not allow other browsers.
I already have a file that opens and closes the tab, however, the tab is closed almost immediately, which does not allow time for the prompt to come on-screen and ask the user if they wish to open or save the file. Here is my code so far:
<html>
<head>
<title>You are being redirected</title>
<meta HTTP-EQUIV="REFRESH" content ="0; url=X:\\Directory\Subdirectory\File.docx">
</head>
<body>
<center>
<script type="text/javascript">
window.open('javascript:window.open("", "_self", "");window.close();', '_self');
</script>
</center>
</body>
</html>
A solution to my problem would be to just literally add a delay between the tab opening and closing in the above code (ideally 5 seconds or 5000 milliseconds). If anyone could show me how to add this delay before it closes that would be extremely helpful.
<script type="text/javascript">
setTimeout(function() {
window.open('javascript:window.open("", "_self", "");window.close();', '_self');
}, 5000);
</script>
You should use settimeout():
setTimeout(function,milliseconds,param1,param2,...)
Read about it here: w3schools settimeout();
This works to open a new tab and close it after 5 seconds
var wnd = window.open("http://google.com", "_blank", "");
window.setTimeout(closeWindow, 5000);
function closeWindow() {
wnd.close();
}
Related
When I call window.location.href("tel:"+'+0060199999') it will open the Skype which is working fine.But i dont want current browser window stay open, i want to close it.If i put close, it is not even open the Skype.Is this possible to do?
<html>
<script>
window.onload = function () {
window.location.href="tel:+0060199999";
window.close();
}
</script>
<body>
Let me test
</body>
</html>
On launching a window using window.open(), a new instance of browser is getting launched on button click every time, even if the calling function is having named window.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function OpenNamedWindow() {
var w2 = window.open("http://stackoverflow.com", "myWindow", "width=500px;height=500px");
}
</script>
<input type="button" onclick="OpenNamedWindow();" value="Launch App" />
</body>
</html>
After launching the page when I try to access window.name, it shows empty.
I did some search and looks like cross domain is causing the issue.
How can I load the page in same instance instead of launching a new browser window all the time?
This is a trick
function openInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
}
But even this, perhaps should not work in all browser depending on users configuration
This topic is still here in stackoverflow.
try to check it by yourself at the following link
Open a URL in a new tab (and not a new window) using JavaScript
It opens only one window at a time:
var window_name = null; // global
...
function open_new_window() {
if (window_name && !window_name.closed) {
window_name.close();
window_name = window.open(....);
}
I am a javascript beginner . I wrote a javascript code to open a website in new window and then get the content of that new window and display it in the first window .. and this is the code :
<!DOCTYPE html>
<html><head></head><body>
<div id="display"></div>
<script type="text/javascript">
var newwin=window.open("http://www.w3schools.com");
newwin.onload=function ()
{display.innerHTML = newwin.document.documentElement.innerHTML;};
</script>
</body></html>
It didn't work .. the web site opened in the new window but the content didn't appear in the first window .. why?
thanks in advance.
Use _parent as a second parameter to position your webpage. (BTW I am assuming you are allowing popups in your browser settings)
var newwin=window.open("http://www.w3schools.com", "_parent");
You cannot do this if the child window is from a different domain. Each domain is sandboxed from the others for security reasons.
There are 2 errors that prevent you:
Different domains
Even if same domain, you set onload function after calling window.open(). So the new "onload" behavior never runs
Solution for same domain:
<!DOCTYPE html>
<html><head></head><body>
<div id="display"></div>
<script type="text/javascript">
var w=window.open("another.html");
var tid = setInterval function () {
if (w.document.readyState !== 'complete')
return;
clearInterval(tid);
display.innerHTML = w.document.documentElement.innerHTML);
}, 100);
</script>
</body></html>
Previously, I got a solution from here on how to open another link automatically on page load. I got this code to do that
window.setTimeout("autoClick()", 2000); // 2 seconds delay
function autoClick() {
var linkPage = document.getElementById('dynLink').href;
window.location.href = linkPage;
}
</script>
dynLink is used in the body as target="_blank" within link tag. But this is loading the desired page within the same tab. Not in a New Tab.
I want when this auto page load clicks the link with id=dynLink, the page opens in a new tab then to load in the same tab.
And I really means New TAB - NOT NEW WINDOW.
Looking forward to some working solution. Thanks!
Following are some of the solutions using plain HTML and JavaScript. You may use based on your requirement and share if you have any other solution.
Open Link Immediately on Page Load.
<body onload="window.open('https://www.google.com/');">
Open link in New Tab on page load.
<body onload="window.open('https://www.google.com/', '_blank');">
Refresh the same URL Using meta tags.
<meta http-equiv="refresh" content="5" />
Refresh a different URL after 5 seconds Using meta tags.
<meta http-equiv="refresh" content="5;URL='https://www.google.com/'" />
Open different URL in New Tab using meta tags.
<meta http-equiv="refresh" content="5;URL=javascript:window.open('https://www.google.com/', '_blank');" />
You can try this:
var newTab = window.open('http://google.at', '_blank');
newTab.location;
Here is the fiddle: http://jsfiddle.net/70kdacL4/310/
For your specific case:
HTML:
<a id="link" href="http://www.google.at" target="_blank">TestLink</a>
JavaScript:
$(document).ready(function(){
window.setTimeout(function(){
var link = document.getElementById("link").href;
var newTab = window.open(link, '_blank');
}, 5000);
});
Be careful if you use a popup blocker. A popup blocker can prevent the tab from beeing opened.
Here is the fiddle: http://jsfiddle.net/qm9ss6s4/4/
I am calling the Javascript window.open() function to load another url in a pop up window.
When the users closes the popup window, I want the MyThanks() function to be called. How I can do this?
My Script :
<!DOCTYPE html>
<html>
<head>
<script>
function openWin(){
myWindow=window.open('http://facebook.com/ekwebhost','','width=600,height=400,left=200');
myWindow.focus();
}
function MyThanks(){
alert("Thanks");
}
</script>
</head>
<body>
<input type="button" value="Open window" onclick="openWin()" />
</body>
</html>
Regarding usage of popups:
Are you aware that Facebook & Twitter still use popup windows for user like & follow. Go to http://www.ekwebhost.com and click the Facebook "Like" button or the Twitter "Follow" button. Why can't I use popup windows for my own needs?
function openWin(){
myWindow=window.open('http://facebook.com/ekwebhost','','width=600,height=400,left=200');
// Add this event listener; the function will be called when the window closes
myWindow.onbeforeunload = function(){ alert("Thanks");};
myWindow.focus();
}
Try this!
You can swap out the function(){ alert("Thanks");}; for MyThanks to call your function.
The new window that you open will have an attribute called opener that references the Window object that created it. You can then call functions on the parent window by calling window.opener.MyThanks();
Popup:
<script type="text/javascript">
function closePopup() {
window.opener.MyThanks();
window.close();
}
</script>
<div>
<h1>My Popup</h1>
<!-- Form or info to display -->
<button onclick="closePopup();">Close</button>
</div>
While there is nothing inherently wrong with browser popup windows, they can be annoying since the user can lose them behind other windows. Also, it takes their focus off of your page to do something else. The application that I currently develop is trying to move all of our popup windows to Javascript dialogs. It gets pretty annoying for users once you get 3 popups deep.
There are a bunch of ways to create dialogs within your webpage. jQuery UI is one library that I like.
You can try this:
<script>
function openWin() {
myWindow = window.open('http://facebook.com/ekwebhost', '', 'width=600,height=400,left=200');
myWindow.focus();
MyThanks();
}
function MyThanks() {
alert("Thanks");
}
</script>