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()
Related
I know that I can use <a href="" target="_blank"> to open a new window. I can also use onclick even handler to create the window and fill it with dynamic content, like win = open(""); win.document.innerHTML =. But, the latter forces me to use button and create window explicitly instead of relying on default <a> functionality. Can I combine them, to use <a> to open the window and it's onclick to fill it?
win.body.innerHTML only works if you open a page that has a body. Some browsers might do that (open a dummy document) for you, but certainly not all
Here is a link - I use inline JS for demonstration, a better choice is to assign the onclick in onload of the page:
Here is a JSFiddle since the SO Snippet does not allow the popup
<a href="activateyourjsandallowpopups.html"
onclick="var w = window.open('','_blank'); // may fail if blocked
if (w) { w.document.write('Hello'); w.document.close(); }
return !w">Click</a>
Perhaps you mean this?
<a href="pagefrommyserver.html" target="myWin"
onclick="setTimeout(function() {
var w = window.open('','myWin'); // get the opened window handle
if (w) { w.document.body.innerHTML = 'Hello'}
},200)">Click</a>
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.
My anchor tag, opens in a new blank window if JavaScript disabled
<a target="_blank" onclick="newWindow(this)" href="http://www.google.co.uk">Google</a>
Here's my external javascript which opens the new window then cancels the original href and target from firing.
function newWindow(element) {
var newWin = window.open(element.href, windowName, "top=0, left=0, height=800,width=700,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no");
return [element.href='Javascript: void()', element.target='_self'];
}
This works ok for 1 click, my problem is that if the user clicks the link again then the href looks like
<a href="Javascript: void();">
so the link wont work!
What am i doing wrong?
How do I code it so once the popup window opens, the original href and target are cancelled?
And the link can be clicked more than once?
thanks
Here is perfectly working solution: http://jsfiddle.net/tceAX/
<a target="_blank" onclick="newWindow(this); return false;" href="http://www.google.co.uk">Google</a>
Javascript:
function newWindow(element) {
var newWin = window.open(element.href, 'window-name', "top=0, left=0, height=800,width=700,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no");
}
You are replacing the link target onClick so just replace
return [element.href='Javascript: void()', element.target='_self'];
with
return false
In your return statement you are setting the element.href property.
element.href='Javascript: void()'
So for the first click it will work, because the element contains the original value. But the second time you click, you already have replaced the href value.
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.
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>