Code after window.open doesn't get executed

2019-07-03 14:37发布

I recently started learning javascript, and I'm currently trying to make a small script to automate a login procedure by filling the user name/password fields, and then clicking the 'Submit'-button.

My code is as follows:

window.open("");
document.getElementById('ctl00_Username').value = "XXXX";
document.getElementById('ctl00_Password').value="XXXX";
document.getElementById('ctl00_ButtonLogin').click();

If I run it once, the site is opened but no text fields are filled.
If I run the code twice (when the site is already opened) the login is successful.

I tried putting "console.log" after "window.open", but for some reason that never seems to get called.

What might I be doing wrong?

Edit: Removed unnecessary code. I am also no longer sure that the document-object actually points to the newly opened window. Calls to "console.log" and "alert" don't seem to do anything, either.

Is it possible to get the correct document-object from the window? Is it even possible to use "window.open" and then access the new document-object? Help would be greatly appreciated!

标签: javascript
2条回答
smile是对你的礼貌
2楼-- · 2019-07-03 15:17

1.Pass your values as query string. Example: www.test.com?username=bro&password=bro. 2.On the other page paste the below code.

$(function () {
        $(document).ready(function () {
        var amount = $('money').val();
        var from = "INR";
        var to = "SGD";
        $.ajax({ type: "POST",
        url: "WebService.asmx/CurrencyConversion",
        data: "{amount:" + amount + ",fromCurrency:'" + from + "',toCurrency:'" + to + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
        var money = $(".money").val();
        $(".money").replace(money, data.d);
        }
        });
        });
        });

3.Now paste this code too.

var uname = getUrlVars()["username"];
var psw = getUrlVars()["password"];

4.You will be having values in the above variables.Enjoy doing whatever you want.

查看更多
看我几分像从前
3楼-- · 2019-07-03 15:40

A reference to the window is returned from the window.open call, you can use it to modify the window.

 win=window.open(...);
 win.document.doYourThing

You also probably need to wait until the document is ready (aka loaded). Using jquery below

 $(win.document).ready(function() {
     //the document is loaded by here, this is probably where you should do your stuff.
 });
查看更多
登录 后发表回答