Haven't been programming in JS for a while.
Now, I have following thing:
<html>
<head>
<script language="JavaScript">
function enlarge()
{
window.close();
}
</script>
</head>
<body>
<img src="addresstomyimg.png" onClick="enlarge()" />
</body>
</html>
(it's very simplified, as, in fact, I have WordPress platform with custom JS modifications etc, but in general, this is the idea).
I don't understand why it's not working.
JavaScript cant close the window unless it opened the window.
In your function, replace window.close() with alert('here') and you'll see the function works fine.
If you want your function to close a window, first open one:
<html>
<head>
<script type="text/javascript">
var popup;
function closewin()
{
popup.close();
}
function openwin()
{
popup = window.open('http://www.google.com');
}
</script>
</head>
<body>
<img src="addresstomyimg.png" onclick="openwin()" /> Click to open, then come back here
<br><br>
<img src="addresstomyimg.png" onclick="closewin()" /> Click to close
</body>
</html>
Demo: http://jsfiddle.net/AlienWebguy/pbFha/
If you try running this script using Firefox and use the Firefox's Error Console to look for errors, you can see that the following error gets logged when you run this script.
Scripts may not close windows that were not opened by script.
You can launch the Error Console in Firefox by pressing Ctrl + Shift + J.
On Chrome, your script successfully closes the tab in which it is running.
Note that the right way to use the <script> tag while writing JavaScript is as follows:
<script type="text/javascript">
If you're trying to close the main window, it won't work.
You can only close windows that were opened by JavaScript.
This code works on IE.
<html>
<head>
<script language="JavaScript">
function enlarge()
{
self.close();
}
</script>
</head>
<body>
<img src="addresstomyimg.png" onClick="enlarge()" />
</body>
</html>
Related
For IE11.
If you open the alert window in the popup window of A, the popup window disappears once, and when you check again, the popup window appears.
For other browsers (Chrome, FF, Edge), the alert appears in the popup window.
I wonder if there is a solution.
Can't reproduce the problem on my side. According to your description, I create a sample using the following sample code, it seems that everything works well:
code in main page:
<button onclick="myFunction()">Open new window</button>
<script>
function myFunction() {
window.open("popuppage.html","_blank","wight=900px, height=600px")
}
</script>
code in popuppage.html:
<head>
<meta charset="utf-8" />
<title></title>
<script>
function myfunction() {
alert("Hello");
}
</script>
</head>
<body>
<a id="btnshowalert" href="#" onclick="return myfunction();" > show alert</a>
</body>
The result as below:
Please try to clear IE browser cache and history, then create a new page to test above code or re-test your code. If still not working, you could try to reset Internet Explorer settings and share your code.
Sorry to ask this again.
I am trying to reload the parent window after closing the son window. The code works fine in IE. but not in Chrome (Version 54.0.2840.99 m).
Is it my Chrome setting problem? Or the code problem?
I searched and tried all the solutions but it still not work. Please assist!! Thanks
<!-- Parent Window -->
<head>
<script language="javascript" type="text/javascript">
function popitup2(url) {
newwindow=window.open(url,'email','top=200,left=500,height=500,width=600');
if (window.focus) {newwindow.focus()}
return false;
}
</script>
</head>
<body >
Call S Method 1
<BR><BR>
<a href="#" onClick="window.open('s.htm', '_blank')">Call S method 2/a>
<p id="demo"></p>
<script>
var d = new Date();
document.getElementById("demo").innerHTML = d;
</script>
</body>
<!--Son window (s.htm) -->
<head>
<script type="text/javascript">
window.onunload = unloadPage;
function unloadPage()
{
window.opener.location.reload();
}
</script>
</head>
<body>
Son !!
</body>
</html>
It's not the window.opener.location.reload() that doesn't work, but it's calling it from the onunload event. There are very strict rules as to what you are allowed to do in that event handler (to prevent spamming and such)
Check this plunker to see what I mean:
https://plnkr.co/edit/RGIrrupeqxd2bmc9M7ex?p=preview
In the example above you can reload the parent page from the child when clicking the link, but the exact same code doesn't work in the unload event.
I'm trying to use javascript to open a popup window. It works in chrome but not in IE or Firefox.
Here is my code for the head section:
<head>
<script type="text/javascript">
function newPopup(url) {
popupWindow = window.open(
url,'popUpWindow','height=400,width=400,left=550,top=200,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
}
</script>
</head>
And my code in the body section:
<body>
<img src="images/banner/buy.gif">
</body
>
And here is a link to the page:
http://riverswest.com/riverswest-site/ambush-jacket.html
The BUY NOW image what you want to click on.
The issue is your target="". You don't need it if you're going to be using javascript popup code.
This works fine:
<img src="images/banner/buy.gif">
In my application i have to alert user to signout on browser close.
For that i have used the javascript as below
<body scroll="no" onbeforeunload="browerClose()">
<script language="JavaScript" type="text/javascript">
function browerClose()
{
window.alert("Click OK to SignOut");
window.location.href = "http://localhost:8086/egs/ervlet?pri=logOut";
}
this is Working for IE ,but not works for FireFox,
Wats the Problem....Any Suggesstions
Thankxx in Advance,
I would suggest you move the javascript function to the head section of the HTML document. That way it is working for Firefox. Maybe this is because HTML documents are processed in sequential order and you need to define the function before you can use it.
Do something like this:
<head>
<script language="JavaScript" type="text/javascript">
function browerClose()
{
window.alert("Click OK to SignOut");
window.location.href = "http://google.com";
}
</script>
</head>
<body scroll="no" onbeforeunload="browerClose()">
<!-- you code here -->
onbeforeunload event will not work from fire fox version 26 if u used any custom alert message in your application which means you need to perform x operation when closing browser/tab or refreshing page but you put alert message in function which will be called from onbeforeunload then the x (update) operation will not happened.
<html>
<head>
<script>
function unloadfunction() {
alert('test');
// update employee ID in Database
}
</script>
</head>
<body onbeforeunload="unloadfunction();">
</body>
</html>
parent.html
<html>
<head>
<script type="text/javascript">
function openWin()
{
myWindow=window.open('child.html','','width=200,height=100');
}
function callback(){
alert("test");
}
</script>
</head>
<body>
<input type="button" value="Open 'myWindow'" onclick="openWin()" />
</body>
</html>
child.html
<html>
<head>
<script type="text/javascript">
window.opener.callback();
</script>
</head>
<body>
</body>
</html>
And the problem is that the child page calls parent's page callback function in FF, IE but not in Chrome.
Any idea ?
Problem happens because of Chrome security error. Domains, protocols and ports must match. But it also happens when page is open from local files system.
Open your page from server, it should be without any problems.
The problem might be the way how chrome run javascript. Chrome sometimes run the js so fast and early and even the DOM is not ready for manipulation. Try this in your child.html
<script type="text/javascript">
setTimeout(function(){window.opener.callback();}, 100);
</script>
I am not sure if this is the exact problem for you, I encountered this problem with jQuery.ready() on chrome.