This seems like it shouldn’t be a difficult task, but I just can’t get my brain to grasp it.
I’m required to have links open in a new window, after first checking whether or not the window is already open.
If the window is open, it should be closed then reopened with the new page loaded.
Personally, I don’t see why it needs to be closed and reopened instead of just reloaded, but that’s what’s required for this task.
I feel like window.open(), window.close() and window.closed are involved, as well as a named window, but I can’t workout how the correct code to implement this. Maybe I’m completely wrong?
I thought of putting the url inside the window.open() as well as the href, but is that unnecessary?
Is there a way to use the href inside of window.url(), or send it there? If I don’t use an href and target in the <a>
that will cause them to be non-functional for users with Javascript disabled right? Should I be using ‘this’ keyword?
Any help would be much appreciated.
Stripped down code is as follows:
items-list.html
<a href=“show-item.html?name=Shirt&price=30 onclick=“checkWindowStatus();”>
<a href=“show-item.html?name=Hat&price=10 onclick=“checkWindowStatus();”>
<a href=“show-item.html?name=Socks&price=5 onclick=“checkWindowStatus();”>
show-item.js
var itemWindow;
function checkWindowStatus() {
// if window is already open
if (!itemWindow.closed) {
// close the window
itemWindow.close();
// open the window again with the new URL
window.open(‘URL’, ‘itemWindow’);
} else { // window isn’t already opened
// open the window
window.open(‘URL’, ‘itemWindow’);
}
}