I want to open popup in another window open popup.
Please check code-
myjavascript.js
var win = null;
function NewWindow(mypage,myname,w,h,scroll){
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
win = window.open(mypage,myname,settings)
}
index.html
<script src="myjavascript.js"></script>
My Account
popup1.html
<script src="myjavascript.js"></script>
Click to open popup2
popup2.html
<h1>You are now popup2.html page</h1>
Above code is working for me but the second popup not open. please check and let me know.
Thanks
#chatfun
The problem is that you're using the same identifier for both window.open(mypage,myname,settings).
Change the name in the second invocation:
index.html
My Account
popup1.html
Click to open popup2
Hope this helps,
Related
I have a help link. If a user clicks on it, it opens a new window with fixed width and height. It's working well except that when I right click the link, there is either no options to 'open in a new tab' (in IE) or I can open in a new tab but is directed to an empty page (chrome). Can any one help to make this like a link and also by default open in a new window (not a tab)?
<html>
<head>
<title>
link
</title>
<script type="text/javascript">
function activateHelpView(helpUri) {
var WindowId = 'SomeWindowId';
var helpWindow = window.open(helpUri, WindowId, 'width=400,height=500,menubar=no,status=no,scrollbars=no,titlebar=no,toolbar=no,resizable=yes');
if (helpWindow) {
(helpWindow).focus();
}
}
</script>
</head>
<body>
<a id='PortOrderPageLearnMoreLink' href='javascript:' title='Learn more' onclick='activateHelpView("http://stackoverflow.com/")'>Learn more</a>
</body>
</html>
Use a real link, not the empty javascript: address. The onclick handler can prevent the link from doing anything "normal", but you'll have something for the right-click to work with.
target=_blank is a strong hint that you want the page opened in a new window, but whether that's honored at all -- and whether in a window or a tab -- is out of the page's control.
<script type="text/javascript">
function activateHelpView(helpUri) {
var WindowId = 'SomeWindowId';
var helpWindow = window.open(helpUri, WindowId, 'width=400,height=500,menubar=no,status=no,scrollbars=no,titlebar=no,toolbar=no,resizable=yes');
if (helpWindow) {
(helpWindow).focus();
}
}
</script>
<a id='PortOrderPageLearnMoreLink' href='http://stackoverflow.com/' title='Learn more' onclick='activateHelpView(this.href); return false;' target='_blank'>Learn more</a>
A more modern way of handling all of this -- particularly if there will be more than one help link -- is to add a class to all of them, and run some JavaScript to add the click handler to each in turn. The HTML stays clean (and with real links, still works if JavaScript is disabled or not loaded).
var helplinks = document.querySelectorAll('.helplink');
for (var i = 0; i < helplinks.length; ++i) {
helplinks[i].addEventListener('click', activateHelpView);
}
function activateHelpView(event) {
event.stopPropagation(); // don't let the click run its course
event.preventDefault();
var helpUri = this.href; // "this" will be the link that was clicked
var WindowId = 'SomeWindowId';
var helpWindow = window.open(helpUri, WindowId, 'width=400,height=500,menubar=no,status=no,scrollbars=no,titlebar=no,toolbar=no,resizable=yes');
if (helpWindow) {
helpWindow.focus();
}
}
<a id='PortOrderPageLearnMoreLink'
href='http://stackoverflow.com/' title='Learn more'
class='helplink' target='_blank'>Learn more</a>
StackOverflow snippets aren't allowed to use some of these functions. A working example can be found here.
I know that this might not be possible.
Lets say I have a list of say 50 hyperlinks. The default behavior is to open the links in a new tab when clicked. But I want to prevent the user from opening 50 tabs if he/she clicks on all 50.
So is there any way to create a hyperlink which when clicked,
Opens the first link in a new tab
Subsequent links are opened in that same tab(instead of a new tab)
If it helps, Chrome will be the browser that we would use for this.
Is there any HTML.JS, Chrome trick that we can use? Thanks.
Add this snippet to your code, and check.
var a = document.getElementsByTagName("a");
for (i=0;i<a.length;i++) {
if (a[i].target="_blank") {
a[i].target="_self"
}
}
This works because it changes the URL of one window if it is opened to the link that the user clicked.
<html>
<head>
<title>
Search Engines
</title>
<script>
opened = false ;
function openMyLink(linkToOpen) {
if (opened === false) {
openWindow = window.open(linkToOpen,"_blank") ;
opened = true ;
openWindow.addEventListener("beforeunload",function () {opened = false ;}) ;
}
else {
openWindow.location.href = linkToOpen ;
}
}
</script>
<style>
a {
cursor : pointer ;
color : blue ;
text-decoration : underline ;
}
</style>
</head>
<body>
<h1>
Search Engines
</h1>
<br><a onclick="openMyLink('http://www.google.com/') ;">Google</a>
<br><a onclick="openMyLink('http://www.bing.com/') ;">Bing</a>
<br><a onclick="openMyLink('http://www.yahoo.com/') ;">Yahoo</a>
</body>
</html>
Requirement:
All child windows which are opened and they are required to remain
open through out the user session on IE irrespective of user action
refreshes the browser windows.
Close all child windows when user click logoff action.
Problem:
Child window handle cannot be retained across the pages when browser re-render the window, due to F5/refresh button or navigation.
Solution:
Open each child window with identical name.
Save the assigned window name in local storage as item in collection or array [or in parent window.name that's the work around if local storage is not acceptable].
//Open popup window "ChildWinName1" from here
window.open("SomeSite/SomeApp/SomePage", "ChildWinName1", "width=600, height=400");
localStorage.setItem("Key1", "ChildWinName1");
//Open popup window "ChildWinName1" from here
window.open("SomeSite/SomeApp/SomePage", "ChildWinName2", "width=600, height=400");
localStorage.setItem("Key2", "ChildWinName2");
Create a blank page in your application which just has self.close method in script.
Page : BlankClose
<!DOCTYPE html>
<html>
<head>
<title>IndexX</title>
<script type="text/javascript">
function Exit() {
self.close();
}
this.Exit();
</script>
</head>
<body>
<div>
Bye..............
</div>
</body>
</html>
Upon logoff, iterate through the collection of window name, for each window name request the blank page[see 3.]. That will close each child window.
function closeAllChildWindow() {
var wins = localStorage.length
for (var i = 1; i <= wins; i++) {
var winName = localStorage.getItem("ChildWinName" + i.toString());
if (winName != null) {
window.open('/SomeSite/SomeApp/blankClose", "Home")', winName);
localStorage.removeItem("ChildWinName" + i.toString())
}
else {
if (localStorage.length > 0) wins++;
}
}
}
Note :
Instead of having multiple keys in local storage, JSON object can be used which has encapsulated string collection.
Below is an example of what I would like but it has two flaws currently.
I believe that the order is incorrect because I cannot see any url sites beyond google.com. Something must be off in the location of certain items in the code. I have tried it without pop-up blockers and still cannot get the other windows to show.
I believe that this program is supposed to open in different windows/tabs. I would like mine to open the next url in the same window and tab and replace the original.
Google replaced by msn; msn replaced by yahoo
I am grateful for the help. Thank you everyone.
code:
<!DOCTYPE>
<html>
<body>
<script>
var urlList = ['http://www.google.com', 'http://www.msn.com', 'http://www.yahoo.com'];
var wnd;
var curIndex = 0; // a var to hold the current index of the current url
function openWindow(){
wnd = window.open(urlList[curIndex], '', '');
setTimeout(function () {
wnd.close(); //close current window
curIndex++; //increment the index
if(curIndex < urlList.length) openWindow(); //open the next window if the array isn't at the end
}, 2000);
}
openWindow();
</script>
</body>
</html>
You can just reuse your window instance:
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var urlList = ['http://www.google.com', 'http://www.msn.com', 'http://www.yahoo.com'];
var wnd;
var curIndex = 0; // a var to hold the current index of the current url
function openWindow(){
wnd = wnd || window.open();
wnd.location.href = urlList[curIndex];
setTimeout(function () {
curIndex++; //increment the index
// If all urls have been showed, close our window instance
if(curIndex < urlList.length) openWindow(); else wnd.close();
}, 2000);
}
openWindow();
</script>
</html>
If you open in the same page your javascript will be gone. It won't work. As soon as google.com is opened that will be the end of your javascript. So you should open in either a new tab or consider using iframes.
See here for more info:
http://www.w3schools.com/tags/tag_iframe.asp
I have a javascript code which opens a new tab in browser from a list each 50seconds, but the browser will crash after 50tabs or more.
so I want to close new tab and open another one each 50 seconds.
my code is:
<html>
<head>
<script>
function openWindow(){
window.open('"about:blank"');
var x = document.getElementById('a').value.split('\n');
atTime = 0;
for (var i = 0; i < x.length; i++) {
if (x[i].indexOf('.') > 0) {
site = x[i];
if (x[i].indexOf('://') < 0) { site = 'http://' + x[i]; }
setTimeout("window.open('" + site + "')", atTime);
atTime += 50000;
}
}
}
</script>
<style>
html, body
{
height : 99%;
width : 99%;
}
textarea
{
height : 80%;
width : 90%;
}
</style>
</head>
<body>
<textarea id="a"></textarea>
<br>
<input type="button" value="Open Windows" onClick="openWindow()">
<input type="button" value="Clear" onClick="document.getElementById('a').value=''">
</body>
</html>
window.open returns a reference to the new window. Call close on that handle.
https://developer.mozilla.org/en-US/docs/Web/API/window.close
note btw:
FAQ
How can I prevent the confirmation message asking the user whether he wants to close the window?
You can not. New windows not opened by javascript can not as a rule be closed by JavaScript. The JavaScript Console in Mozilla-based browsers will report the warning message: "Scripts may not close windows that were not opened by script." Otherwise the history of URLs visited during the browser session would be lost.
window.close() should work to close tabs you opened (but it's not possible to close a tab you didn't open)
in fact it can't be closed unless it was opened by a script but there is a way to fool the browser into thinking that's the case:
window.open('','_parent','');
then you can use
window.close();
Putting it together:
<script language="javascript" type="text/javascript">
function closeWindow() {
window.open('','_parent','');
window.close();
}
</script>
reference: http://www.yournewdesigner.com/css-experiments/javascript-window-close-firefox.html
to close a firefox tab use
window.close()
this will close the current window.