As you are new to JavaScript, it's best not to pick up bad habits right from the start. One of the worst practices that continues to this day is using inline HTML event attributes (onclick
, onmouseover
, etc.). There are many reasons not to use them and you can see them here.
Next, don't use the a
element when you aren't actually navigating anywhere (after all, you are setting the href
attribute to #
to indicate that you don't want to actually go anywhere). It is semantically incorrect and can cause problems for people who rely on screen readers to visit your page. Just about any element can have a click
event, so a div
or button
element would do the trick.
Or, if you wanted to dispense with the need to create and open the new window yourself, then a
would be appropriate and all you'd need is:
<a href="test.html" target="_blank">link</a>
The target="_blank"
will cause the page to open in a new browser window at full size.
Next, the file needs to be injected into the function from somewhere, this is a great use-case for HTML data-*
attributes. You can have multiple links that call your function but each can store a different page that should be used in the function. When you need to get the actual value from the attribute, you can use the dataset
object to extract it.
Lastly, since you are showing that you want the iframe
to fill up the entirety of the new window, you don't really need an iframe
at all, just populate the new window with your desired page.
Following modern standard, the code would be (the window will not actually open in the code snippet below due to the restrictions on code in Stack Overflow, but you can see a working version here):
// First, get a reference to the HTML element you wish to work with.
// There are many ways to do this (via an id, via a CSS selector, via
// the element's position in the document) with .getElementById() or
// .querySelector(), etc. Here, I've used an id in the HTML, so we'll
// get the element by its id:
var link = document.getElementById("openIFrame");
// Next, register the event handling function with the event
link.addEventListener("click", myFunctionA);
function myFunctionA() {
// When the function is invoked, the keyword "this" will be bound to the HTML
// element that triggered the event. From there, you can access your custom
// data property with: dataset.propName which only needs to be passed to your
// window.open invocation since the only thing you want in the new window is
// the contents of the new file:
var myWindow = window.open(this.dataset.file, "MsgWindow", "width=1000,height=700");
}
<div id="openIFrame" data-file="test.html">link</div>
But, if you do need the iframe
, you'd just concatenate the same this.dataset.file
into the string that creates the iframe
:
// First, get a reference to the HTML element you wish to work with.
// There are many ways to do this (via an id, via a CSS selector, via
// the element's position in the document) with .getElementById() or
// .querySelector(), etc. Here, I've used an id in the HTML, so we'll
// get the element by its id:
var link = document.getElementById("openIFrame");
// Next, register the event handling function with the event
link.addEventListener("click", myFunctionA);
function myFunctionA() {
// When the function is invoked, the keyword "this" will be bound to the HTML
// element that triggered the event. From there, you can access your custom
// data property with: dataset.propName which only needs to be concatenated
// into your string for the iframe:
var myWindow = window.open("", "MsgWindow", "width=1000,height=700");
myWindow.document.write('<iframe name="contentwin" src="' +
this.dataset.file +
'" scrolling="yes" frameborder="0" height="100%" width="100%"></iframe>');
myWindow.document.close();
}
<div id="openIFrame" data-file="test.html">link</div>