Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access elements across frames?

file1.html

<html>
<head>
<title>AIDS (Automated ID System)</title>
<HTA:APPLICATION 
    id="frames" 
    border="thin" 
    caption="yes" 
    icon="http://www.google.com/favicon.ico" 
    showintaskbar="yes" 
    singleinstance="yes" 
    sysmenu="yes" 
    navigable="yes" 
    contextmenu="no" 
    innerborder="no" 
    scroll="auto" 
    scrollflat="yes" 
    selection="yes" 
    windowstate="normal" />

<script language="javascript" type="text/javascript">

    function pausecomp(millis) 
    { 
        var date = new Date(); 
        var curDate = null; 
        do { curDate = new Date(); } 
        while(curDate-date < millis); 
    } 

    function getWindowsUserName()
    {
        var WinNetwork = new ActiveXObject("WScript.Network");
        var urlToSite = createCustomURL(WinNetwork.UserName);
        var frame = document.getElementById("psyncLink"); 
        frame.onload = function() { 
            frame.onload = null; 
            if (requestingPassword()) { 
                //alert("password button screen");
                passwordButtonScreen();
            } else { 
                alert("direct password required"); 
            } 
        } 
        frame.src = urlToSite;
    }

    function requestingPassword()
    {       
        var btn = window.frames[1].document.getElementsByName("SUBMIT-password.pss");
        if (btn.length == 0) {
            return false;
        } else {
            return true;
        }
    }

    function passwordButtonScreen()
    {       
        var btn = window.frames[1].document.getElementsByName("SUBMIT-password.pss");       
        btn[0].click();     
    }

    function createCustomURL(userName)
    {
        var customURL = "http://localhost/nph-psf.exe?HOSTID=AD&ALIAS=" + userName;
        return customURL;
    }

    function Sleep(milliseconds) {
        var start = new Date().getTime();
        for (var i = 0; i < 1e7; i++) {
            if ((new Date().getTime() - start) > milliseconds){
                break;
            }
        }
    }

    function whichScreen() { 
        var btn = window.frames[1].document.getElementsByName("SUBMIT-password.pss"); 
        if (btn.length == 0) { 
            alert("no button!"); 
            // User is at password screen
            var textField = window.frames[1].document.getElementsByName("_MYPW");
            textField[0].text = "";
            return; 
        } else { 
            btn[0].click();
            WaitSeconds(5);
            var textField = window.frames[1].document.getElementsByName("_MYPW");
            textField[0].value = "ios12sdk";
            btn = window.frames[1].document.getElementsByName("SUBMIT-VERIFY");
            btn[0].click();
        }
    }

    var loadOtherFrame = function (text) { 
        getWindowsUserName(); 
        alert(text);
    };

</script>

    </head>     
    <frameset cols="300px, *"> 
        <frame src="leftframe.html" name="topo" id="topo" application="yes" /> 
        <frame src="topo1.htm" name="psyncLink" id="psyncLink" application="yes" /> 
    </frameset>     
</html> 

leftframe.html

<html>

<head>
  <title>AIDS&nbsp;Assistant</title>
</head>
<script language="javascript">

function checkPassword() {
  var validString = /^[a-z](?=[a-z]*[0-9])[a-z0-9]{0,6}[a-z]$/;
  if (validString.test(document.getElementById("newPassword").value)) {
    alert("The password is valid");
    var validate = function () {
        // validate textbox input ...
        // call parent page function
        parent.loadOtherFrame(document.getElementById("newPassword").value);
    };
  } else {
    alert("The new password does NOT meet the requirements. Please try again.");
  }
}



</script>

<body>
    <table width="300px">
        <tr>
            <td>Type Your Old Password</td>
            <td><input id="oldPassword" type="text" maxlength="8" /></td>
        </tr>
        <tr>
            <td>Please type your new password</td>
            <td><input id="newPassword" type="text" maxlength="8" min="8" /></td>
        </tr>
        <tr>
            <td colspan="2"><input id="checkOldPassword" type="button" title="Check New Password" value="Check New Password" onclick="checkPassword()" /></td>
        </tr>
    </table>

</body>

</html>

Let me clarify what I am doing

  1. leftframe.html needs to validate a textbox
  2. Once the left frame has been processed. It needs to notify the parent window, file1.html, so it can load the second frame (topo1.htm)

topo.htm doesn't exist so the HTA app loads Page could not be found for that specific frame but it should change when we call function getWindowsUserName() from loadOtherFrame. The loadOtherFrame is called from the leftframe.html file as tjscience indicated.

When I run the HTA, the loadOtherFrame is being called (before the button click on leftframe.html)

like image 786
Cocoa Dev Avatar asked Jun 19 '12 15:06

Cocoa Dev


2 Answers

I assume your framesetis not in the frame already. You can access all frames via top.window.frame_name from any frame or top.window itself.

form_in_psyncLink=top.window.psyncLink.document.getElementById('form_id');

EDIT

If this external page is not in the same domain, you can't acces it's content by regular means. Read more about Same origin policy for JavaScript. (AFAIK HTA doesn't change this behavior.)

My example above gives you a reference to the psyncLink, naturally you'll use it in your validating script to access elements in psyncLink to "paste" data. (which in this case maybe impossible due to the cross-domained pages)

like image 130
Teemu Avatar answered Sep 28 '22 04:09

Teemu


Edit to include passing text input along to parent page

You can call a function in the parent page from the frame when the textbox is validated:

JS IN Leftframe.html

<script type="text/javascript">
    var validate = function () {
        // validate textbox input ...
        // call parent page function, passing the text from the input
        parent.loadOtherFrame(text);
    };
</script>

JS in file1.html

<script type="text/javascript">
    var loadOtherFrame = function (text) {
        // load other frame here ...
    };
</script>
like image 25
xcopy Avatar answered Sep 28 '22 04:09

xcopy