在window.open之后获取html代码

时间:2014-04-05 12:30:06

标签: javascript html

我正在尝试在窗口加载window.open

后获取窗口的html代码

但它实际上并不起作用。我想这个问题是关于我如何管理我的onload功能,但我不明白这里的问题,我的console.log中没有显示任何内容(我没有错误)

我的HTML代码:

<html>
    <head>
        <script type="text/javascript" src="js/ftp.js"></script>
        <script type="text/javascript" src="js/jquery-1.11.0.js"></script>
        <title>FTPtoolJS</title>
    </head>

    <body bgcolor='#ffffff'>

    <p><table border='0' cellpadding='0' cellspacing='1'
              align='center' width='98%'>
        <form name=login>
            <tr><td align='right'>&nbsp;Username&nbsp;</td>
                <td>&nbsp;<input type='text'
                                 name='username' value='username' size='20'
                                 maxlength='20'>&nbsp;&nbsp;</td></tr>
            <tr><td align='right'>&nbsp;Password&nbsp;</td>
                <td>&nbsp;<input type='text'
                                 name='password' value='**********' size='20'
                                  maxlength='20'>&nbsp;&nbsp;</td></tr>
            <tr><td align='right'>&nbsp;Server&nbsp;</td>
                <td>&nbsp;<input type='text'
                                 name='server' value='ftp.adresse.com' size='60'
                                 maxlength='120'>&nbsp;&nbsp;</td></tr>
            <tr><td align='right'>&nbsp;Directory&nbsp;</td>
                <td>&nbsp;<input type='text'
                                 name='directory' size='60'
                                 maxlength='200'>&nbsp;&nbsp;</td></tr>
        </form>
    </table><hr>

    <table border='0' cellpadding='0' cellspacing='0' width='100%'>
        <tr>
            <td align='right'>
                <input style='color:#000000' type='button' value='Connect' onClick='Login(this.form)'>
            </td>
        </tr>
    </table>

    </body>
</html>

我的JS代码:

function Login(form) {
    var username = form.username.value;
    var password = form.password.value;
    var server = form.server.value;
    var directory = form.directory.value;

    if (username && password && server) {
        FTPConnect(username, password, server, directory);
    }
    else {
        alert("error");
    }
}

function FTPConnect(username, password, server, directory){
    var url;
    var w;

    if(directory != undefined){
        url = "ftp://" + username + ":" + password + "@" + server + "/" + directory;
        w = window.open("ftp://" + username + ":" + password + "@" + server + "/" + directory, '_blank',
            'toolbar=yes,location=yes,status=yes,' +
                'scrollbars=auto,copyhistory=no,menubar=no,width='
                + ((screen.AvailWidth/2)-12) + ',height='
                + (screen.AvailHeight-124) +',left=' + ((screen.AvailWidth/2))
                + '),top=0,resizable=yes');
    }
    else {
        url = "ftp://" + username + ":" + password + "@" + server;
        w = window.open("ftp://" + username + ":" + password + "@" + server, '_blank',
            'toolbar=yes,location=yes,status=yes,' +
                'scrollbars=auto,copyhistory=no,menubar=no,width='
                + ((screen.AvailWidth/2)-12) + ',height='
                + (screen.AvailHeight-124) +',left=' + ((screen.AvailWidth/2))
                + '),top=0,resizable=yes');
    }

    w.onload = function() {
        console.log(w.documentElement.outerHTML); //here nothing is displayed in console.log
    }
}

1 个答案:

答案 0 :(得分:1)

快速浏览一下您的代码以及以下内容

w = window.open("ftp://" + username + ":" + password + "@" + ...

未通过same origin policy测试,因为

  1. 协议不匹配
  2. 看起来server也可能是另一个主持人
  3. 在这种情况下,您也无法通过 CORS 启用支持,因为访问 Window XMLHttpRequest更严格


    它看起来像用户名&amp;密码不会使原点测试失败