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?
Asked
Active
Viewed 1,521 times
0

Little Alien
- 1
- 8
- 22
-
win.document.write, not innerHTML. And you can use the onclick of a link the same way you use a button – mplungjan Jun 14 '16 at 16:02
-
@mplungjan 1. `win.document.body.innerHTML='abc'` works fine for me. 2. It is not an issue. 3. What does it mean `the same way as button`? Where did I demonstrate using button's href to open a new windo? – Little Alien Jun 14 '16 at 16:10
-
@mplungjan I tried `button onclick="win = window.open('', '_blank'); win.document.body.innerHTML='abc'` and it works. Now, I try to factor out `window.open` into the . – Little Alien Jun 14 '16 at 16:12
1 Answers
0
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>

mplungjan
- 169,008
- 28
- 173
- 236
-
I do not get it. Why do you open two windows? Do you understand that I wanted to replace `window.open` with `activateyourjsandallowpopups` rather than to have both of them? – Little Alien Jun 14 '16 at 16:17
-
I do NOT open two windows. If the window.open fails, then I go to the hmtl page that explains that the user needs to allow popups. – mplungjan Jun 14 '16 at 16:18
-
Ok, thanks. It makes sense now. But, it is not what I asked. Probably you say that what I ask is impossible in indirect way. – Little Alien Jun 14 '16 at 16:27
-
Then I do not understand what you ask. What is the exact set of events you want when clicking on the link? A html page opens AND you write something somewhere on the page? Perhaps if you use setTimeout but the page MUST come from the page server as the script – mplungjan Jun 14 '16 at 16:28
-
-
Eliminate what? Remember I still am not sure I understand your use case – mplungjan Jun 14 '16 at 17:58
-
When you replace a thing you add one and eliminate another. When I ask to replace explicit window.open with anchor's click (which naturally opens a window), I ask to eliminate the window.open. – Little Alien Jun 14 '16 at 18:14
-