This is what I am trying to do: Once submit a form on my main JSP/HTML page a new page with some link opens. When I click on any of the link the link should open in the parent Window. I.e the Window I started from. How do i do this?
Use window.opener.location.href in javascript
For example,
Click me!
You'll need JavaScript for this. HTML's target can't target the window's parent (opener) window.
The following will open the page in the parent window if JavaScript is enabled, and open it in a new window if it is disabled. Also, it will react gracefully if the current page does not have an opener window.
<a href="page.php"
onclick="if (typeof window.opener != 'undefined') // remove this line break
window.opener.location.href = this.href; return false;"
target="_blank">
Click
</a>
MDC Docs on window.opener
Use parent.location.href if it's on the same page in an iframe. Use opener.location.href if it's another entire tab/window.
Related
From my webpage, I am opening a new page in different tab. When the page in different tab will be loaded, I want to close my webpage i.e. Suppose I am on pageA and I opened pageB using window.open(). Now, when pageB will be opened, I want to close pageA. I tried this jsFiddle -
function onClickBtn()
{
var win = window.open('http://www.google.com','_blank','');
setTimeout(function () {
win.close();
}, 5000);
};
This is my HTML markup -
<input id="btn1" type="button" value="Click me" onclick="onClickBtn()"/>
However, this code is closing pageB ,not PageA.
I have tried a jsFiddle https://jsfiddle.net/gdso9eeg/
Please suggest me a suitable solution.
It is doing what it should do.
You opened the new window with that variable and on using close() with it, it is closing the window which it opened.
If you want to close the parent then open the new page on the parent.
Try this:
In place of _blank put _parent
If page is redirecting on the same page window then window.close will not work due to some browser security for this scenario type we need to use one of the following way.
1.window.history.back();
2.document.referrer
3.Request.UrlReferrer server side
The .close() should follow Javascript's window, not win.
Try
window.close();
Good luck!
EDIT:
Have you checked this post window.close and self.close do not close the window in Chrome ?
"..javascript must not be allowed to close a window that was not opened by that same javascript."
It also offers some workarounds to the issue.
Is it possible to refresh a page from another page using Javascript or JQuery without opening the same page in a new tab.
JS:
var newtab = window.open('http://localhost:8081/app/home');
newtab.document.location.reload(true);
I tried the above, but here, it will open a new tab, with the same page, which is already opened in the browser.
Please suggest a method.
I got the idea from a previous Question , here they used window Object Reference to reload the popup window, but for me it wont work, because, the parent window and child window runs in 2 different ports. So using the same trick, what i did is :
HTML:
<a onclick="openNewTab()">app2</a>
<a onclick="refreshExistingTab()">Refresh</a>
JS:
<script>
var childWindow = "";
var newTabUrl="http://localhost:8081/app/home";
function openNewTab(){
childWindow = window.open(newTabUrl);
}
function refreshExistingTab(){
childWindow.location.href=newTabUrl;
}
</script>
refreshExistingTab() this instend of refreshExistingTab
take a look at https://developer.mozilla.org/en-US/docs/Web/API/Window.open
basically if you do window.open and specify a window name it will overwrite that window with the url you provided.
so if you open the page each time with same window name, it should overwrite it each time you do it again from that other page.
In JS I am using :
window.open('index.php?module=Contacts&action=UD_lookupemailtemplates&tempmode=userdefined&temp_url='+encodeURIComponent(temp_php_url),'emailtemplate','top=100,left=200,height=400,width=500,resizable=yes,scrollbars=yes,menubar=no,addressbar=no,status=yes')
To open a small window which has few links.
Now on the click on any one link an id will be attached to the a url and then I used this :
window.document.location.href = url;
This is also in JS. what this does is that it changes the contents of the small popup window but I need it to close the popup window and open this url in the main parent window from where the popup was created. Is that possible ?
Suppose
var popup_window = window.open(...);
For closing this use
popup_window.close();
and to redirect the parent windows
window.parent.location.href = url;
To close a popup that you opened, store the return value from window.open:
var popup = window.open(...);
Then, you can use:
popup.close();
I'm not clear based on your question where the URL variable is stored (in which window), but you can reference window variables in the popup via popup.variableName.
First you have to redirect parent window using
window.opener.location.href = "http://url.com";
and than close the current (popup) window
window.close();
Personaly I don't like pop up windows instead of that maybe yo can do the following:
You can open an overlay DIV that you can populate with data from "index.php?module=Contacts..." using AJAX and make links inside of that DIV that way so that by clicking them you close that overlay DIV and redirect page to what you want...
This line is from my JSP file.
I opened this page by setting a target attribute in the <a> tag like target ="_TOP", but the code below is not working. How can I close this window?
<div>click</div>
You can only close a window with JavaScript that has been opened with JavaScript. Since you went with an HTML method, this won't work.
However, if you were to re-code so that JavaScript was opening the window instead...
mylink
Then you could close the resulting window with JavaScript.
target should be _top - this will not pop a NEW window but overwrite the current - is that what you want
try this
<a href="page.jsp" target="_blank"
onclick="var w=window.open(this.href,this.target); return w?false:true">pop a new window</a>
or
<a href="page.jsp" target="mywin"
onclick="var w=window.open(this.href,this.target); return w?false:true">pop a new window</a>
the later you can get the handle:
var w = window.open('','mywin');
if (!w.closed) w.close()
How do I open a new window in javascript properly?
I've tried:
<a href='javascript:window.open("testing.html","mywindow","menubar=0,resizable=1,width=200,height=500,left=0,top=0");' >New Window</a>
This causes a new window to pop up but in Firefox it leaves the original page with a blank window saying "[object Window]". I've also had issues with IE as well.
How can I open a window that works in Firefox, IE, Safari and Chrome?
Bonus: If you can help in creating a link that is javascript degradable (I think that's the term).
By putting the script in the href property, the link will follow the string representation of the return value of the script.
The most backwards compatible way is to put the url and target in the link as regular attributes, and use the onclick event to open a window instead of following the link. By returning false from the event you prevent the link from being followed:
New Window
If Javascript is disabled in the browser, it will follow the link instead and open it in a new window.
Use the target _blank to open a new window.
javascript: links are pretty old-school. Try it with an onclick attribute. And don't forget to return false in the onclick handler to prevent the main page from following the link too.
New Window
you could add void(0) to your javascript code which will prevent the browser from doing anything to the current window
<a href='javascript:window.open("testing.html","mywindow","menubar=0,resizable=1,width=200,height=500,left=0,top=0"); void(0)' >New Window</a>
<a href='javascript:(function(){window.open("testing.html","mywindow","menubar=0,resizable=1,width=200,height=500,left=0,top=0")}();' >New Window</a>
Or make it a function:
File
<script>window.onload = function(){
document.getElementsByTagName('a').onclick =
function(evt){ el = evt.target; if (el.className == 'popup'){
window.open(el.href,"mywindow","menubar=0,resizable=1,width=200,height=500,left=0,top=0");
return false; }; };</script>