tags:

views:

106

answers:

2

Hi, i cant get my main form's button from frame by javascript.I am getting button's ID by querystring then i execute following script but cant get button.When i write parameter's name such as getElementByID('btnDelete'),it founds control?What can be reason and how can i solve this problem?

       function okay() {  
        var btn = getQuerystring('btn');
        window.parent.document.getElementByID(btn).click();

}

function getQuerystring(key, default_) {
            if (default_ == null) {
                default_ = "";
            }
            var search = unescape(location.search);
            if (search == "") {
                return default_;
            }
            search = search.substr(1);
            var params = search.split("&");
            for (var i = 0; i < params.length; i++) {
                var pairs = params[i].split("=");
                if (pairs[0] == key) {
                    return pairs[1];
                }
            }
            return default_;        }
+1  A: 

I believe there is a typo in your code. It should be window.parent.document.getElementById(btn).click(); with a lowercase "d".

John Riche
No,in visual studio quick watch window when i assign value 'btnDelete',i can get it.I changed getElementByID to getElementById but didnt solve my problem.
Alexander
A: 

Save this as test.html:

<html>
    <body>
        <form action="http://google.com"&gt;
            <input type="submit" id="thebutton" value="Click Me!"/>
        </form>
        <iframe src="test2.html?btn=thebutton"/>
    </body>
</html>

Save this as test2.html:

<html>
    <head>
        <script>
            function okay() {  
                var btn = getQuerystring('btn');
                window.parent.document.getElementById(btn).click();
            }

            function getQuerystring(key, default_) {
                if (default_ == null) {
                    default_ = "";
                }
                var search = unescape(location.search);
                if (search == "") {
                    return default_;
                }
                search = search.substr(1);
                var params = search.split("&");
                for (var i = 0; i < params.length; i++) {
                    var pairs = params[i].split("=");
                    if (pairs[0] == key) {
                        return pairs[1];
                    }
                }
                return default_;
            }
        </script>
    </head>
    <body onLoad="okay()">
    </body>
</html>

The only issue was that you needed a lower case d in getElementById.

Jeremy Stein