Jump to content

Commuication between windows


ameliabob
 Share

Recommended Posts

I am trying to write something with two windows but with common code used by both. I have put together a simple example bu the alert never occurs

 

<!DOCTYPE html>
<html>
<body>

<p>Click the button to open a new window called "MsgWindow" with some text.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    var myWindow = window.open("", "MsgWindow", "width=200, height=100");
    myWindow.document.write("<p>This is 'MsgWindow'. I am 200px wide and 100px tall!</p>");
    myWindow.document.write("<button onclick='BackHere()'>Return</button>");
}
function BackHere(){
    alert("I made it!");
}
</script>

</body>
</html>

 

What linkage am I missing?

 

Thanx

Link to comment
Share on other sites

The most immediate solution would be this:

myWindow.document.write("<button onclick='window.opener.BackHere()'>Return</button>");

The BackHere() method belongs to the original window, so you need to reference the method that's on that window.

Link to comment
Share on other sites

i dont know why you are trying to do it that way but here you go you need to have the fuction backhere insite the window code :

<!DOCTYPE html>

<body>

<p>Click the button to open a new window called "MsgWindow" with some text.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    var myWindow = window.open("", "MsgWindow", "width=200, height=100");
    var d='';
    myWindow.document.write("<p>This is 'MsgWindow'. I am 200px wide and 100px tall!</p>");
    myWindow.document.write("<button onclick='BackHere()'>Return</button>");
     myWindow.document.write('<sc'+'ript>function BackHere(){alert("I made it!");} </sc'+'ript>');
}


</script>
</body>

good luck

Link to comment
Share on other sites

I tried to use the first method and I go the message "Permission denied to access property "SendOffRequest".

 

The first code window is the main calling program. The user pushes [ctrl][t] and the second code window is loaded. when one of the buttons is clicked the error message occurs. The highlighted in Bold which calls the BOLD Italics and then the error.

 

[codewindow]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<script type="text/javascript">
var CtrlPushed=false;

function CheckKey(e){
var keynum;
if(window.event) // IE
keynum = e.keyCode;
else if(e.which) // Netscape/Firefox/Opera
keynum = e.which;
if(keynum==17) { // Chceck for Ctrl pushed
CtrlPushed = true; // 17 is ctrl
}

if( CtrlPushed ){ // Only look at these if user has initiated the CTRL sequence
switch (keynum){
case 80: // Test for P or
case 84: // test for T
ptWindow= window.open("http://test.comsum.atflorida.us","ptWindow");
break;
}
}
}

function SendOffRequest( whatIsIt){
//alert(whatIsIt);
if (window.XMLHttpRequest) {
myRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) {
myRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
if(myRequest){
var url = "comsumProcess.php?function=" + whatIsIt + "&uDate=" +uDate+"&device="+device+ "&userID=" + userID+"&uProfile="+uProfile;
lastSentRequest = url; // Save this for any error message
url += "&sid=" + Math.random();
myRequest.open("GET", url, true);
}
// STUFF DELETED HERE AS IT DOESNT'T APPLY
</script>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="computer.css" />
<title>Commodity Summary</title>


</head>
<body onkeydown="CheckKey(event)">

<div id="header">
<p><br /></p>
<h1 id="hdgline" style="font-size:36px">Trend Tracker SQL version </h1>
<p><br /></p>


<div id="hdgbtns" align='center'>
</div>
</div>
<div class="content" id="content">
</div>

</body>

</html>

[/codewindow]

SECOND CODEWINDOW

[codewindow]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
var s;
var userID="prdtst";
var uProfile="WhatIf";
var uDate="2009-09-07";
var uLastAction;
var device="computer";

var OFFSET=5;
/*
function SendOffRequest( whatIsIt){
document.getElementById("function").value=whatIsIt;
document.getElementById("submitForm").submit();
}
*/
function CaseSetup(){
window.opener.SendOffRequest("casesetup");
}

function EndSetup(){
window.opener.SendOffRequest("casefinish");
}
function DeleteTC(s){
var a = s.split(",");
var t = "Confirm that you want to delete Test Case number "+a[1]+" "+" named "+a[2]+"<br>"+a[3];
if (confirm(t)==true)
window.opener.SendOffRequest("deleteTC&tcnumber="+a[0]);
}
function ListCases(){
window.opener.SendOffRequest("listcases");
}

function ShowHomeScreen(){
var s = "<button id= 'Button' onclick='CaseSetup()' >Define Testcase</button>";
s += "<button id= 'mainButton' onclick='RunCases()' >Run Testcase(s)</button>";
s += "<button id= 'mainButton' onclick='ListCases()' >List Testcases</button>";
document.getElementById("hdgbtns").innerHTML = s;
}
function ShowDetails(num){
document.getElementById("num").value = num;
SendOffRequest("showdetails");
}

function StartSetup(){
var tnumber = ptWindow.document.getElementById("testno").value;
var tname = ptWindow.document.getElementById("testname").value;
var tdesc = ptWindow.document.getElementById("testdesc").value;
var x = ptWindow.document.getElementById("userPtr").selectedIndex;
var y = ptWindow.document.getElementById("userPtr").options;

// alert("Index: " + y[x].index + " is " + y[x].text);
SendOffRequest("startsetup&number="+tnumber+"&name="+tname+"&desc="+tdesc+"&acct="+y[x].text);
}
</script>
<head>

<link rel="stylesheet" type="text/css" href="comsumtest.css" />
<title>Test Library</title>


</head>
<body onload="ShowHomeScreen()">

<div id="header">
<p><br /></p>
<h1 id="hdgline" style="font-size:36px">Comsum Test Library </h1>
<p><br /></p>


<div id="hdgbtns" align='center'>
</div>
</div>
<form id="submitForm" action="testFunctions.php" method="get">
<input id = "function" type="text" name="function" hidden>;
<input id = "num" type="text" name="numb" hidden>
<div class="content" id="content">
</div>
</form>
</body>

</html>

[\codewindow]

 

Now what have I done wrong.

Link to comment
Share on other sites

What browser are you using and where are you running this script from? Some browsers block AJAX on the local filesystem. I don't remember which ones for sure, but I think IE is one of them.

 

EDIT:

Try doing just a simple alert instead of sending the AJAX request. That will confirm whether the code is working or not.

Edited by ShadowMage
Link to comment
Share on other sites

I just reread the error message, and I misunderstood the message you got. I see now that it says it cannot access the function as a property of the opening (parent) window. JavaScript doesn't work cross-domain (for example, from www.website1.com to www.website2.com). I suspect that the blank URL in your window.open command is making JavaScript think it's going cross-domain. You might need to create a blank page within your site and use that URL in your open command.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...