I want to open a new window and send message to this window and then receive in another tab. I am doing this but it is not working.
if (id > 0) {
var w = window.open(testurl,"address");
w.postMessage("ddd", "address");
}
new window is open but data is not showing.
Here is another window content.
<script>
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event) {
alert(event.data);
}
</script>
Data is not alert. What is the problem?
targetOrigin is not the same as the name of the window you're opening. Origin is the value you get when alerting document.origin in the opened window.
This is what you should be passing as the second value to postMessage; not the name of the window.
Related
I am using a thirdparty tool that integrated in my html (PHP) as IFrame solution. (Recommendation of the manufacturer).
<iframe id="theIframe" src="manufacture.com"> </ iframe>
A pop-up is built up and redirect to a new popup with the login screen. Anyone has idea, how I get can capture all events from all popup. I need the reference from the login-screen or the close event of them.
How I can check or capture that this event.
The Iframe-initiated pop will be redirected to a second popup (for whatever reason) and will not bind the second popup to the iframe and will not get to the events.
if (window.attachEvent) {
window.attachEvent("onmessage", receiveMessage);
} else {
console.log("event message added");
window.addEventListener("message", receiveMessage, false);
}
function receiveMessage(event) {
//capture the event for closing the second popup
console.log(event);
alert("User has closed the window in the iFrame");
//Close iFrame
var iframe = document.getElementById('theIframe');
iframe.parentNode.removeChild(iframe);
}
}
In short, the problem is not really a problem.
There is no solution to this problem. I integrated the url of the second pop directly into the iframe. Sometimes the solution is so close.
Sometimes it just helps to rethink the problem.
So I'm trying to test whenever popup windows are enabled for my site. My window should be blocked by browser's popup blocker, unless they are already allowed. If popup windows are allowed everything works fine, since I can test whenever the window was opened directly in my parent page.
Problem is that I still want to mark the test as sucessfull if the user allows to open popup window later by allowing the popup window to open. Only way for me to do that is by calling some function on my parent page, but window opened this way has null window.opener and I can't set the object on child window directly from my parent window since I don't know when will it open.
Is there any way to get reference to my parent window from child window when it's opened this way?
Or any other workaround that could call function on my parent window whenever the child window is sucessfully opened?
Maybe event event that I could hook up to whenever such window is opened, but I couldn't find any such event.
Sample code:
function () {
var myWin = window.open('popupWin.html', '_blank', 'resizable=no,scrollbars=no,height=1,width=1', true);
if (!myWin || myWin == null || typeof (myWin) == 'undefined')
SetPopupResult(false);
else {
SetPopupResult(true);
myWin.close();
}
}
popupWin.html is empty page with following script:
<script>
window.opener.SetPopupResult(true); //throws Cannot read property 'SetPopupResult' of null
setTimeout(function () {
window.close();
}, 1000);
</script>
Edit: Forgot to mention this is issue in Google-Chrome so far, firefox doesn't seem to have this problem and window.opener is correct, IE's behaivor is different as it refreshes the page and openes the popup window like they were enabled.
(1) i have a script that opens a new window (2)New window can change its location/url for some redirection (3) I want to execute window.postMessage to the final redirection page of the popup window which will the parent window will received
<script>
//open window
//pops will change its url for some redirection
var pops = window.open('http://est.com/test.php', 'Login with Test', "width=300px;heigth=500px;");
window.addEventListener("message",function(e) {
if (e.origin !== 'http://west.com') {
return;
}
alert(e.data);
},false);
</script>
<script>
//code below is from other origin 'http://est.com/finalredirect.php'
// 'http://est.com/finalredirect.php' is the final redirection page of the popup window
window.postMessage({'data': 'test'}, 'west.com');
</script>
Or is there another work around for this?
Have a flash player that pops out into a separate popup browser window. And on the source page the flash player just displays a message that it is currently popped out.
Now if the user navigates away from the source page (to another page on the same domain) how do i get a reference to the popup or just detect if its open (using javascript on the new page)?
Figured this out. Had to have a javascript timer in the popped out window that trys to execute a function in the parent window that if exists will simply hide the player.
example
Popout window script
<script>
if(window.opener!=null)
setTimeout("popCheck();",2000);
function popCheck()
{
//Use try catch to prevent premission denied msgs in
//case parent window is on different domain
try
{
window.opener.setPoppedOut();
}
catch(e)
{
}
//Check Every 2 seconds
setTimeout("popCheck();",2000);
}
window.onbeforeunload = function()
{
//Used to facilitate popin on window close
if(window.opener!=null)
{
try
{
window.opener.setPoppedIn();
}
catch(e)
{
}
}
}
</script>
Parent window script
<script>
//Save the previous contents so that we can restore them later.
window.popoutContent = document.getElementById("popoutContent").innerHTML;
function setPoppedOut()
{
document.getElementById("popoutContent").innerHTML = "Player is Popped Out";
}
function setPoppedIn()
{
document.getElementById("popoutContent").innerHTML = window.popoutContent;
}
</script>
So here is the thing happening .I click on submit button it goes to some.php and gets the json value with item2 and we open print.php in a new window .After that the page which generated print.php gets refreshed with a function. Now my problem i want to retain the print.php window on the top and also refresh the back window which generated it .So i went and used window.location = self.location();.Now my print.php stays on top but the page which needs to be refreshed gives me error "object doesnot support this action"
$("#submit-button").click(function(){
$.post("some.php?id="+id1value,function(data){
if(data.item2 !=null){
window.open('print.php?id1='+id1value+'&id2='+ data.item2 );
refreshBack($("#div1").text());
}else{
//Do Process for else
} },'json')
}
)
});
function refreshBack(text){
if(text==="abc"){
url = "one.php";
}
else{
url = 'two.php?id3='+id3value;
}
document.form_name.action=url;
document.form_name.submit();
}
To refresh the owning window that launched a popup, use:
window.opener.location.reload();
To refresh a child window, save the reference made by window.open() and later call the reference's location reload.
var myPopup = window.open('print.php?id1='+id1value+'&id2='+ data.item2 );
...
myPopup.location.reload();
To give a particular window foreground focus, use the .focus() method in either window object.
// Focus the current window
window.focus();
// Focus the child window
myPopup.focus();
// Focus the window that opened the current popup
window.opener.focus();