调整来自不同域的 url 的窗口大小时,javascript 访问被拒绝

发布于 2024-10-16 16:05:41 字数 785 浏览 2 评论 0原文

我有一个自定义函数,可以从不同的网址打开一个到屏幕中心的窗口。在我当前的情况下,我正在打开我的域之外的网址。这是我的职责。

function wopen(url, name, w, h) {
    w += 32;
    h += 96;
    wleft = (screen.width - w) / 2;
    wtop = (screen.height - h) / 2;
    if (wleft < 0) {
        w = screen.width;
        wleft = 0;
    }
    if (wtop < 0) {
        h = screen.height;
        wtop = 0;
    }
    var win = window.open(url,
                name,
                'width=' + w + ', height=' + h + ', ' +
                'left=' + wleft + ', top=' + wtop + ', ' +
                'location=no, menubar=no, scrollbars=yes');
    // +
    //'status=no, toolbar=no, scrollbars=no, resizable=yes');
    win.resizeTo(w, h);
    win.moveTo(wleft, wtop);
    win.focus();
}

这在 IE6 和 FF 上完美运行,但在 IE7 上不起作用

I have a custom function that will open a window to the center of the screen from a different url. On my current case I am opening a url outside my domain. This is my function.

function wopen(url, name, w, h) {
    w += 32;
    h += 96;
    wleft = (screen.width - w) / 2;
    wtop = (screen.height - h) / 2;
    if (wleft < 0) {
        w = screen.width;
        wleft = 0;
    }
    if (wtop < 0) {
        h = screen.height;
        wtop = 0;
    }
    var win = window.open(url,
                name,
                'width=' + w + ', height=' + h + ', ' +
                'left=' + wleft + ', top=' + wtop + ', ' +
                'location=no, menubar=no, scrollbars=yes');
    // +
    //'status=no, toolbar=no, scrollbars=no, resizable=yes');
    win.resizeTo(w, h);
    win.moveTo(wleft, wtop);
    win.focus();
}

This works perfectly on IE6, and FF but not on IE7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

非要怀念 2024-10-23 16:05:41

问题在于您尝试打​​开具有单独域的窗口,这在 IE7 及更高版本中被视为安全问题。本质上,当您打开该新窗口时,它会创建一个新进程并使您的进程保持独立,因此您无法再操作该其他窗口。

http://social.msdn.microsoft .com/Forums/en/iewebdevelopment/thread/e9cebb92-f943-4a79-b29b-7376039ea6a0

http://msdn.microsoft.com/en-us/library/Bb250462.aspx

因此,一旦您使用与您自己的域不同的域打开新窗口,您就会失去对它的控制。我看不出有什么办法可以在不调整最终用户计算机的情况下改变这一点。

编辑

嗯,显然你可以通过打开一个你可以控制的窗口来解决这个问题,然后将 window.location.href 更改为你的网址。试试这个:

function wopen(url, name, w, h) {
    w += 32;
    h += 96;
    wleft = (screen.width - w) / 2;
    wtop = (screen.height - h) / 2;
    if (wleft < 0) {
        w = screen.width;
        wleft = 0;
    }
    if (wtop < 0) {
        h = screen.height;
        wtop = 0;
    }
    var win = window.open('about:blank', // <- Note about:blank
                name,
                'width=' + w + ', height=' + h + ', ' +
                'left=' + wleft + ', top=' + wtop + ', ' +
                'location=no, menubar=no, scrollbars=yes');
    // +
    //'status=no, toolbar=no, scrollbars=no, resizable=yes');
    win.location.href = url;
    win.resizeTo(800, 150);
    win.moveTo(wleft, wtop);
    win.focus();
}
wopen('http://www.yahoo.com/', 'yahoo', 250, 250);

我不知道这是否是一个黑客;我很惊讶它是如此容易解决,至少可以更改窗口大小等。但是,它可以工作(至少在 IE8 上)。

The issue is that you are attempting to open a window with a separate domain, which in IE7 and higher is considered a security issue. Essentially, when you open that new window, it creates a new process and leaves your process separate, so you can no longer manipulate that other window.

http://social.msdn.microsoft.com/Forums/en/iewebdevelopment/thread/e9cebb92-f943-4a79-b29b-7376039ea6a0

http://msdn.microsoft.com/en-us/library/Bb250462.aspx

So, once you open that new window with a domain different from your own, you lose control of it. I don't see a way to change this without adjusting the end-users computer.

EDIT

Hmm, apparently you can get around this by opening a window that you do have control of, then changing the window.location.href to your url. Try this:

function wopen(url, name, w, h) {
    w += 32;
    h += 96;
    wleft = (screen.width - w) / 2;
    wtop = (screen.height - h) / 2;
    if (wleft < 0) {
        w = screen.width;
        wleft = 0;
    }
    if (wtop < 0) {
        h = screen.height;
        wtop = 0;
    }
    var win = window.open('about:blank', // <- Note about:blank
                name,
                'width=' + w + ', height=' + h + ', ' +
                'left=' + wleft + ', top=' + wtop + ', ' +
                'location=no, menubar=no, scrollbars=yes');
    // +
    //'status=no, toolbar=no, scrollbars=no, resizable=yes');
    win.location.href = url;
    win.resizeTo(800, 150);
    win.moveTo(wleft, wtop);
    win.focus();
}
wopen('http://www.yahoo.com/', 'yahoo', 250, 250);

I don't know if this is a hack or not; I'm surprised it's that easy to get around, at least for changing window resize and whatnot. But, it works (at least on IE8).

美煞众生 2024-10-23 16:05:41

浏览器和操作系统会检查任何窗口的许多安全事项。

对于这种情况,我不确定,但也尝试一下。
如果在通过 js 代码调整浏览器窗口大小时单击并按住鼠标按钮,那么您将收到拒绝访问错误。

原因是当真实的物理用户准备好鼠标拖动事件时,操作系统拒绝此类活动。

见下面的网址
http://prcoldfusion.blogspot.com/2012/06 /access-denied-javascript-error-internet.html

there are many security things that a browser and os checks for any window.

for this case, i am not sure but try this also.
if your mouse button is clicked and hold on at the time of resizing browser window through your js code then you will get access denied error.

reason is OS denied such activities when real physical users is ready for mouse drag event.

see below url
http://prcoldfusion.blogspot.com/2012/06/access-denied-javascript-error-internet.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文