简体   繁体   English

Javascript弹出式登录跨浏览器问题

[英]Javascript pop-up login cross-browser issue

I have a page with an iframe in it. 我有一个包含iframe的页面。 Within the iframe, the default page runs some javascript to open a child window, login to an external service, then display some content from that external service back in the iframe. 在iframe中,默认页面运行一些javascript来打开子窗口,登录到外部服务,然后在iframe中显示该外部服务中的某些内容。

My code works fine in Chrome and IE but in Safari and Firefox it does not. 我的代码在Chrome和IE中可以正常工作,但在Safari和Firefox中却不能。 In those browsers the parent window seems to ignore that fact that the user is now logged in to the external service and displays their "Not logged in" message instead of the info that is supposed to display. 在这些浏览器中,父窗口似乎忽略了以下事实:用户现在已登录到外部服务,并显示其“未登录”消息,而不是应该显示的信息。

Here's the code for what I'm trying to do. 这是我正在尝试执行的代码。

HTML (main page): HTML(主页):

<iframe id="brief" name="brief" src="contents/somepage.php?var=WXYZ" width="962" height="600">
<p>Your browser does not support iframes.</p>
</iframe>

somepage.php somepage.php

<html>
<head>
<script type="text/javascript">
window.onload = function()
        {
var code = 'WXYZ';
var login = http://www.externalsite.com/brief/html.asp?/cgi-bin/service?msg=0048&usr=username&pwd=pass';

//OPEN CHILD WINDOW AND LOGIN USING VARIABLES ABOVE, THEN CLOSE WINDOW
childWindow=window.open(login,'','width=30,height=30,location=no');
var cmd = 'childWindow.close()';
setTimeout(cmd,2000);

//REDIRECT THIS IFRAME TO BRIEFING INFORMATION
var uri = 'http://www.externalsite.com/brief/html.asp?/cgi-bin/service?msg=0092&AID=NOSEND&MET=1&NTM=1&LOC='+code;
self.location.href=uri;
}
</script>
</head>

<body>
&nbsp;
</body>
</html>

I have tried adjusting various setTimeout functions to try to delay certain aspects of the script to wait for something else to happen but it doesn't seem to help. 我尝试调整各种setTimeout函数,以尝试延迟脚本的某些方面以等待其他事件发生,但这似乎无济于事。

Is there some cross-browser problem with this code that I am missing? 我缺少此代码的跨浏览器问题吗?

Thanks 谢谢

setTimeout(cmd,2000); is not going to block script execution for two seconds; 在两秒钟内不会阻止脚本执行; rather, it sets up an event that will fire in approximately 2 seconds. 而是设置了一个事件,该事件将在大约2秒钟内触发。 Immediately after your call to setTimeout , the remaining parts of the script will execute: 在调用setTimeout ,脚本的其余部分将立即执行:

// This happens right away
uri = 'http://www.externalsite.com/brief/html.asp?/cgi-bin/service?msg=0092&AID=NOSEND&MET=1&NTM=1&LOC='+code;
self.location.href = uri;

The fact that it works in any browser is just lucky timing. 它可以在任何浏览器中运行的事实只是幸运的时机。 If you want the iframe to refresh after the popup closes, add that code to your callback (you don't need to and shouldn't use a string for your timer handler, by the way): 如果您希望在弹出窗口关闭刷新iframe,请将该代码添加到回调中(顺便说一句,您不需要,也不应使用字符串作为计时器处理程序):

setTimeout(function() {
    childWindow.close();
    var uri = 'http://www.externalsite.com/brief/html.asp?/cgi-bin/service?msg=0092&AID=NOSEND&MET=1&NTM=1&LOC='+code;
    self.location.href = uri;
}, 2000);

Even this solution won't work if the popup's content doesn't load within two seconds. 如果弹出窗口的内容在两秒钟内未加载,则即使此解决方案也无法使用。 The best solution is to have the popup close itself after loading. 最好的解决方案是使弹出窗口在加载后自行关闭。 Then, you could detect when it closes and know that you're ready to reload your iframe. 然后,您可以检测到它何时关闭,并知道您已准备好重新加载iframe。 But if this is a third-party site that you don't have control over, you're probably stuck using a less-than-ideal solution like this. 但是,如果这是您无法控制的第三方站点,那么您可能会陷入这种不太理想的解决方案的困境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM