I have a page that was dynamically made using javascript's window.open();
function. This opens a new window with a URL of about:blank
and then I inject the rest of the code into the page. I'm trying to detect whether or not the user refreshes the page and then in a later script tell the opener that the task was completed (hence why this page isn't a static one, it needs to communicate with another open tab)
The problem is, is doesn't seem to be setting the hash on the end of the URL. I'm not even sure if its even possible to do this since it's not an actual URL.
if (document.location.hash=="#one") {
alert('Good Job!')
} else {
document.onunload=function() {
window.location.href = window.location.href+encodeURIComponent("#one");
}
}
I had used encodeURIComponent()
because i had heard somewhere that it was good practice to do so in case of non-alphanumerics. I'm not sure if this is the problem or if I should have used href=#one
but neither seemed to work.
Any solutions?
encodeURIComponent
converts characters with special meaning in a URL (such as #
) to encoded versions that don't have that special meaning, so that is the problem (because you need the special meaning "starts the fragment identifier").
one
has no special characters, so it doesn't need encoding. If you were taking input and you didn't know if it contained special characters then you would exclude the # from the encoding.
window.location.href + "#" + encodeURIComponent(some_string_here);