Jump to content
maniootek

_IEBodyWriteHTML with javascript

Recommended Posts

maniootek

I have html file with source code:

<script>
function highlight_row() {
    var table = document.getElementById('mytable');
    var cells = table.getElementsByTagName('td');

    for (var i = 0; i < cells.length; i++) {
        var cell = cells[i];
        cell.onclick = function () {
            var rowId = this.parentNode.rowIndex;

            var rowsNotSelected = table.getElementsByTagName('tr');
            for (var row = 0; row < rowsNotSelected.length; row++) {
                rowsNotSelected[row].style.backgroundColor = "";
            }
            var rowSelected = table.getElementsByTagName('tr')[rowId];
            rowSelected.style.backgroundColor = "red";
        }
    }

}

window.onload = highlight_row;
</script>

<table id="mytable"><tr><td>Click me and my background become red</td></tr><tr><td>Click me and my background become red</td></tr></table>

which works when I open it with Internet Explorer (when I click on the text, the background become red). I want to load same page on the autoit.

I have tried this code but javascript doesn't work (after click on text nothing happens)

#Include <IE.au3>

$html_source = "" & _
'<html><head></head><body>' & _
'<table id="mytable"><tr><td>test</td></tr><tr><td>test2</td></tr></table>' & _
'' & _
'</body></html>'

$oIE = _IECreate()
_IEBodyWriteHTML($oIE, $html_source)
$script = "" & _
"function highlight_row() {" & _
"    var table = document.getElementById('mytable');" & _
"    var cells = table.getElementsByTagName('td');" & _
"    for (var i = 0; i < cells.length; i++) {" & _
"        var cell = cells[i];" & _
"        cell.onclick = function () {" & _
"            var rowId = this.parentNode.rowIndex;" & _
"            var rowsNotSelected = table.getElementsByTagName('tr');" & _
"            for (var row = 0; row < rowsNotSelected.length; row++) {" & _
"                rowsNotSelected[row].style.backgroundColor = '';" & _
"            }" & _
"            var rowSelected = table.getElementsByTagName('tr')[rowId];" & _
"            rowSelected.style.backgroundColor = 'red';" & _
"        }" & _
"    }" & _
"}" & _
"window.onload = highlight_row;"
IEHeadInsertScript($oIE, $script)

Func IEHeadInsertScript($o_object, $s_script)
    Local $o_head, $o_element
    $o_head = _IETagNameGetCollection($o_object, "head", 0)
    $o_element = $o_object.document.createElement('script')
    $o_element.type = 'text/javascript'
    $o_element.text = $s_script
    Return $o_head.appendChild($o_element)
EndFunc

Any idea?

Share this post


Link to post
Share on other sites
Danp2

Use _IEDocWriteHTML instead of _IEBodyWriteHTML --

#Include <IE.au3>

$html_source = "" & _
'<html><head>' & _
'<script>' & _
"function highlight_row() {" & _
"var table = document.getElementById('mytable');" & _
"var cells = table.getElementsByTagName('td');" & _
"for (var i = 0; i < cells.length; i++) {" & _
"var cell = cells[i];" & _
"cell.onclick = function () {" & _
"var rowId = this.parentNode.rowIndex;" & _
"var rowsNotSelected = table.getElementsByTagName('tr');" & _
"for (var row = 0; row < rowsNotSelected.length; row++) {" & _
"rowsNotSelected[row].style.backgroundColor = '';" & _
"}" & _
"var rowSelected = table.getElementsByTagName('tr')[rowId];" & _
"rowSelected.style.backgroundColor = 'red';" & _
"}" & _
"}" & _
"}" & _
"window.onload = highlight_row;" & _
'</script>' & _
'</head><body>' & _
'<table id="mytable"><tr><td>test</td></tr><tr><td>test2</td></tr></table>' & _
'' & _
'</body></html>'

$oIE = _IECreate()
_IEDocWriteHTML($oIE, $html_source)

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.