Access Denied - How to set document.domain when writing to new window

J

johkar

I am getting an Access denied error when I write to a new window. The
situation and code are outlined below. I am setting the domain in the
main window. The problem is that the window I am popping up never gets
it domain set, even though I have coded it to be the same as the main
window. I get an Access Denied errror.

Four different domains:
*localhost.mycompany.com
*test.mycompany.com
*pilot.mycompany.com
*mycompany.com

Page 1 on localhost.mycompany.com has its domain set via script:
document.domain="mycompany.com"; This is the main window

Page 2 - Written to the popup window below also is also set to the same
domain. Upon submit, Page to calls page 3 which is name lookup
servlet. Page 3 returns the value to Page 1 (main window).

<script type="text/javascript">
winstr += '<html><head><title>my App<\/title>';
winstr += '<script
type="text\/javascript">document.domain=\'mycompany.com\'<\/script>';
winstr += '<\/head>';
winstr += '<body>';
winstr += '<form name="myForm" method="get" action="default.do">';
winstr += '<div class="textbold">Last Name:<\/div>';
winstr += '<input type="text" name="searchFilter" id="searchFilter"
size="26" \/>';
winstr += '<input type="submit" value="Search" \/><\/div>';
winstr += '<\/form>';
winstr += '<\/body>';
winstr += '<\/html>';
var w=220;
var h=120;
var winname='popWindow';
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
var winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl +
',toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no';
if(win==null || win.closed)//only launch window if it doesn't already
exist
win = window.open("", winname, winprops);
win.focus();
windoc = win.document;
//win.document.domain='mycompany.com';
windoc.write(winstr);
windoc.close();
</script>
 
T

Thomas 'PointedEars' Lahn

johkar said:
I am getting an Access denied error when I write to a new window. The
situation and code are outlined below. I am setting the domain in the
main window. The problem is that the window I am popping up never gets
it domain set, even though I have coded it to be the same as the main
window. I get an Access Denied errror.

Of course. document.domain works if the second-level domain of both
resources is the same. Since a generated document has no second-level
domain, you cannot set its document.domain. That said, I do not think
you need to.
[...]
<script type="text/javascript">
winstr += '<html><head><title>my App<\/title>';
winstr += '<script
type="text\/javascript">document.domain=\'mycompany.com\'<\/script>';
winstr += '<\/head>';
winstr += '<body>';
winstr += '<form name="myForm" method="get" action="default.do">';
winstr += '<div class="textbold">Last Name:<\/div>';
winstr += '<input type="text" name="searchFilter" id="searchFilter"
size="26" \/>';
winstr += '<input type="submit" value="Search" \/><\/div>';
winstr += '<\/form>';
winstr += '<\/body>';
winstr += '<\/html>';

Eeek. Provided that `winstr' was declared before, consider

winstr += new Array(
'<html><head><title>my App<\/title>',
'<script type="text/javascript">',
'document.domain="mycompany.com";',
'<\/script>',
'<\/head>',
'<body>',
'<form name="myForm" action="default.do">',
'<div class="textbold">Last Name:<\/div>',
'<input name="searchFilter" id="searchFilter" size="26">',
'<input type="submit" value="Search"><\/div>',
'<\/form>',
'<\/body>',
'<\/html>'
).join("");

instead.
var w=220;
var h=120;
var winname='popWindow';
Unnecessary.

var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;

Think about said:
var winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl +
',toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no';
if(win==null || win.closed)//only launch window if it doesn't already
exist

if (!win || win.closed)

BTW, source code should be posted so that it can be executed as-is, also
meaning that it should not break when word-wrapped. Such (long) inline
comments should be before-comments, not after-comments.
win = window.open("", winname, winprops);
win.focus();

IMO, you should wait with focusing until you have written the content.
And you should feature-test the focus() method before you call it.
windoc = win.document;

You should declare _all_ your identifiers.
//win.document.domain='mycompany.com';

See above.
windoc.write(winstr);
windoc.close();
</script>


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,836
Messages
2,569,748
Members
45,545
Latest member
rapter____0

Latest Threads

Top