最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Permission denied after window.open() in IE7 - Stack Overflow

programmeradmin1浏览0评论

We have a winforms application with an embedded IE control.

In this IE control, we run a web application (I control the web application but not the winforms application).

In the web application, I run some javascript to open a sub-window and populate it with HTML:

    var features = "menubar=no,location=no,resizable,scrollbars,status=no,width=800,height=600,top=10,left=10";
    newTarget = "reportWin" + String ( Math.random () * 1000000000000 ).replace( /\./g ,"" );
    reportWindow = window.open('', newTarget, features); 
    var d = reportWindow.document; // <-- Exception is thrown here
    d.open();
    d.write('<head>\r\n<title>\r\n...\r\n</title>\r\n</head>');
    d.write('<body style="height: 90%;">\r\n<table style="height: 100%; width: 100%;" border="0">\r\n<tr>\r\n<td align="center" valign="middle" style="text-align:center;">\r\n');
    d.write(...);
    d.close();

When we run the web application within this WinForms app (but not by itself nor in another WinForms app) we get a Javascript error at the indicated line:

Line 0: Access denied

Any ideas on why this could be happening or on how I could avoid it? Note that the window is not opening a URL; it's just an empty window.

From the same application, opening a window with a specified URL in the same domain does work.

We have a winforms application with an embedded IE control.

In this IE control, we run a web application (I control the web application but not the winforms application).

In the web application, I run some javascript to open a sub-window and populate it with HTML:

    var features = "menubar=no,location=no,resizable,scrollbars,status=no,width=800,height=600,top=10,left=10";
    newTarget = "reportWin" + String ( Math.random () * 1000000000000 ).replace( /\./g ,"" );
    reportWindow = window.open('', newTarget, features); 
    var d = reportWindow.document; // <-- Exception is thrown here
    d.open();
    d.write('<head>\r\n<title>\r\n...\r\n</title>\r\n</head>');
    d.write('<body style="height: 90%;">\r\n<table style="height: 100%; width: 100%;" border="0">\r\n<tr>\r\n<td align="center" valign="middle" style="text-align:center;">\r\n');
    d.write(...);
    d.close();

When we run the web application within this WinForms app (but not by itself nor in another WinForms app) we get a Javascript error at the indicated line:

Line 0: Access denied

Any ideas on why this could be happening or on how I could avoid it? Note that the window is not opening a URL; it's just an empty window.

From the same application, opening a window with a specified URL in the same domain does work.

Share Improve this question edited Sep 4, 2012 at 17:58 antlersoft asked Sep 4, 2012 at 17:39 antlersoftantlersoft 14.8k4 gold badges38 silver badges56 bronze badges 2
  • Are you sure this isn't a popup-blocker in action? – Josh Stodola Commented Sep 4, 2012 at 17:51
  • It doesn't appear to be pop-up blocker because the window does open. The problem is trying to access the document object within the winodw. – antlersoft Commented Sep 10, 2012 at 16:10
Add a ment  | 

1 Answer 1

Reset to default 5 +50

Based on:

  1. IE 6/7 Access Denied trying to access a popup window.document
  2. http://thecodecave./2006/07/20/how-to-get-around-access-is-denied-in-a-windowopen-javascript-call/

The problem you are having is that the Url being opened needs to be on the same domain as the page that is opening it. Presumably a blank Url will not share the domain of its creator. I wrote a couple of quick test web pages and found

  1. Calling var reportWindow = window.open('', newTarget, features); resulted in the access denied error.
  2. Same thing with var reportWindow = window.open('http://google.', newTarget, features);
  3. But opening up another page in the site which does the rendering does work var reportWindow = window.open('WebForm2.aspx', newTarget, features);

This last one popped up a window pointing to WebForm2.aspx which executed this code:

window.document.open();
window.document.write('<head>\r\n<title>\r\n...\r\n</title>\r\n</head>');
window.document.write('test<body style="height: 90%;">\r\n<table style="height: 100%; width: 100%;" border="0">\r\n<tr>\r\n<td align="center" valign="middle" style="text-align:center;">\r\n');
window.document.close();
发布评论

评论列表(0)

  1. 暂无评论