Does JS work in popup windows? - javascript

I want a popup window containing a canvas, but I'm unable to get the JS to work in the popup.
Here's a toy program that shows the problem, trying to get 'alert' to work in the popup.
<html> <head> <title> popup test </title>
<script language="javascript">
function popup()
{OpenWindow=window.open("","","height=250,width=250");
OpenWindow.document.write("<html><body bgcolor=pink>Hello!");
OpenWindow.document.write("<form><input type='button' value='alert' onclick='doalert()'></form> ");
OpenWindow.document.write("</body></html>");
}
function doalert() { alert("Got in");}
</script> </head>
<body>
<input type="button" onclick='popup()' value="click to see new window">
</body></html>
The alert button shows in the popup window, blinks when I click it, but doesn't put up the alert box.

Define doalert in the child window:
<html> <head> <title> popup test </title>
<script language="javascript">
function popup() {
OpenWindow=window.open("","","height=250,width=250");
OpenWindow.document.write("<html><body bgcolor=pink>Hello!");
OpenWindow.document.write('<script language="javascript">function doalert() { alert("Got in"); } <\/script>');
OpenWindow.document.write("<form><input type='button' value='alert' onclick='doalert()'></form>");
OpenWindow.document.write("</body></html>");
}
</script> </head>
<body>
<input type="button" onclick='popup()' value="click to see new window">
</body></html>

The "doalert()" must be initiated only when the new window get loaded. Put these kind of functions which are needed to be work in parent window and child window in a function and trigger them when each window opens are when new codes are inserted or included to a page.

Related

Cannot open a separate window as an underlying tab in HTML

I am trying to open a separate window (as an underlying window) of the current window. I have attached a simple code segment which I used. But in Chrome there is a popup blocked message appeared when I do this. It means that The separate window is not recognized as a window, it still considered as a popup. How can I do this ? Any suggestions would be really appreciated
<html>
<head>
<title>JS Window example</title>
</head>
<script type="text/javascript">
function windowonload()
{
window.open("http://yahoo.com", "sameera", "height=200,width=200");
}
</script>
<body onload="javascript: windowonload()">
<h1>JS Window example</h1>
</body>
</html>
you can use iframe tag like this wherever you want to show the window
<iframe src="http://yahoo.com"></iframe>

How do I edit the html in a popup window?

SO, I know how to create a popup window in google, and I know how to write in the popup window, but how do I put in other HTML tags in a popup window?
The Html code that I have is below.
<html>
<head>
<script>
function openWin()
{
myWindow=window.open("","","width=200,height=100");
myWindow.document.write("<p>NEW WINDOW<p>");
}
</script>
</head>
<body>
<button onmousedown='openWin();'>Hello</button>
</body>
</html>
As you can see, in the document.write function, I have two <p> tags, and I can also use the <button> tag, but I cannot use the <script> tag, does anyone know why, or if it is possible?
So you're trying to write a <script> tag to your popup window? You have to do it like this:
<html>
<head>
<script>
function openWin()
{
myWindow=window.open("","","width=200,height=100");
myWindow.document.open();
myWindow.document.write("<p>NEW WINDOW<p>");
myWindow.document.write("<scr" + "ipt>alert('Script works!')</scr"+"ipt>");
myWindow.document.close();
}
</script>
</head>
<body>
<button onmousedown='openWin();'>Hello</button>
</body>
</html>
Otherwise the parent window will think the script tags are something it should interpret.
You cannot write "<script>" or "</script>" to the window, these are specifically disallowed. You have to split up these terms.
function openWin() {
myWindow=window.open("","","width=200,height=100");
myWindow.document.write("<p>NEW WINDOW<p>");
myWindow.document.write("<scr");
myWindow.document.write("ipt>alert('Script works!')</scr")
myWindow.document.write("ipt>");
}

How to disable right click on a hyperlink in page/popup open in Iframe using javascript/jquery?

I need to disable right click on a Anchor tag/hyperlink in page/popup open in iframe using javascript/jquery?
I have below code logic which works when I open page in iframe but not work when I open popup in Iframe by clicking on link within the Iframe page.
In below example when I right-click on Open Popup link its not allowing the right click but when I open popup by clicking Open Popup link it's not working for links in popup.
Any suggestions?
Below sample code snippet for reference -
TestPage.html
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<title>Test Page</title>
</head>
<body>
<iframe id="sampleIframe" src="PopupPage.html" width="400" height="150"></iframe>
<script language="javascript" type="text/javascript">
$('#sampleIframe').load(function() {
$('#sampleIframe').contents().find('a').each(function() {
$(this).on("contextmenu", function() {
return false;
});
});
});
</script>
</body>
</html>
PopupPage.html
<html>
<head>
<title>Popup Page</title>
</head>
<body>
You are on popup page.
Open Popup
<script language="javascript" type="text/javascript">
function openPopup() {
window.open("LinkPage.html", null,
"height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
}
</script>
</body>
</html>
LinkPage.html
<html>
<head>
<title>Link Page</title>
</head>
<body>
Goto Google
Goto Facebook
</body>
</html>
For this to work, you need to include your script to disable the links in the actual iFrame content.
To demonstrate, create a jsfiddle with this code (it will not let me save it to post a link)
This would be your "LinkPage.html"
HTML
Open Popup
JAVASCRIPT
function openPopup() {
window.open("http://jsfiddle.net/t45qvtap/1/show", null,
"height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
}
And you will see it working.
Just to explain further, this is the contents of the fiddle that the above code will open in an iFrame, this would be your "PopupPage.html"
HTML
This content is in an iframe
<div>
Goto Google
Goto Facebook
</div>
JAVASCRIPT
$('body').contents().find('a').each(function() {
$(this).on("contextmenu", function() {
return false;
});
});

Is it possible to take the FB share dialog and after a user has shared, to then post a message to the users?

For example, the dialog below will open a new window to share from. After the window is closed out because the user has shared the post. Could you then do a overlay message from the original url using javascript?
http://www.upworthy.com/if-you-live-in-one-of-these-states-the-impact-of-washingtons-shenanigans-is-huge
The site above does something like this, just not sure how. If you click on share to facebook and then "x" out of the box, you'll see a overlay that happens after a user has either shared or closed the window. Is this done with javascript, if so how?
<a class="shareonfb" style="color: #fff;text-indent: 0px;padding-left: 35px;width: 130px;padding-top: 10px;height: 24px;" onClick="window.open (this.href, 'child', 'height=400,width=665,scrollbars'); return false" href="https://www.facebook.com/sharer/sharer.php?u=http://foo.com" target="_blank">Share on Facebook</a>
create a html file "parent.html" and put the code:
<!DOCTYPE html>
<html>
<head>
<script>
function open_win()
{
myWindow=window.open('popup.html','','width=300,height=250');
myWindow.focus();
myWindow.onbeforeunload = WindowCloseHanlder;
}
function WindowCloseHanlder(){
myWindow.close();
window.alert('Popup is closed');
}
</script>
</head>
<body>
<h1>Welcome to my Home Page</h1>
Share on Facebook
</body>
</html>
create "popup.html" and put this code:
<!DOCTYPE html>
<html>
<head></head>
<body>
<h1>I am pop up</h1>
<p>Close this window</p>
</body>
</html>
You will see the javascript call after you close the popup window. Instead of alert message you can call your popup.

Popup window unload issue

I have one popup window, when user opens and closes popup window simultaneously, it is giving script error. Below code is used in popup window.
<html>
<head>
<script src="../../StaticContent/Scripts/jquery-1.7.2.min.js"
type="text/javascript"></script>
<script src="../../StaticContent/Scripts/jquery-ui-1.8.20.custom.js"
type="text/javascript"></script>
<script type="text/javascript" language="javascript">
function CloseWarning() {
return "Do you want to close the window";
}
function onload()
{
}
</script></head>
<body onbeforeunload="return CloseWarning();" onload="onload();">
//Some html
<script type="text/javascript" language="javascript">
$(document).ready(
function() {
//Some code
});
</script>
</body>
</html>
I have tried degubbing, it is giving error at $(document).ready() line. I think because of closing the popup window before fully loading HTML is causing this problem.
Any clues???
you have ()() as double in the function declaration. change like below.
function CloseWarning(){
return "Do you want to close the window";
}

Categories