Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Attempting to set the click for an element via JavaScript but having some trouble. The event is to be set when an aspx web page loads. However, it's executing the event on page load rather than when the element is actually clicked. Lastly, the click event is not working after the page is loaded. I've tried setting the event using both the addEventListener and click methods but to no avail. Here's a sample of the code:

HTML
<a class="HelpContext" title="Help" id="NonModalHelpContext" style="removed30px;">?</a>


VB
ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "SetHelpContext", "setHelpContext('" & sPid & "');", True)


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


    function setDefaultInvoice(aspCheckBoxID) {
        
        oRegExp = new RegExp(aspCheckBoxID)                        
        //window.alert(aspCheckBoxID)

        for (i = 0; i < document.forms[0].elements.length; i++) 
        {
            elm = document.forms[0].elements[i]
            if (elm.type == 'checkbox' && elm.id.indexOf('grdInvoiceTemplates') > -1) 
            {
                //window.alert(elm.id)
               // window.alert(elm.name)
                                
                if (oRegExp.test(elm.id)) {                
                    elm.checked = true;
                }
                else {
                    elm.checked = false;
                }    
            }
        }
    }


// Moved below
//    function setHelpContext(sUrl) {
//        var ctrl = window.parent.document.getElementById('NonModalHelpContext');
//        ctrl.addEventListener("click", window.open("HelpPage.aspx?" + sUrl, "HelpPage", "width=500,height=300,toolbar=no,scrollbars=yes,resizable=no,dependent=yes", false), false);
//    }
</script>


<html xmlns="http://www.w3.org/1999/xhtml" >
......

</html>


<script language="javascript" >
    // Get a reference to the PageRequestManager.
    var prm = Sys.WebForms.PageRequestManager.getInstance();

    // Using that prm reference, hook _initializeRequest
    // and _endRequest, to run our code at the begin and end
    // of any async postbacks that occur.
    prm.add_initializeRequest(InitializeRequest);
    prm.add_endRequest(EndRequest);

    // Executed anytime an async postback occurs.
    function InitializeRequest(sender, args) {
        // Change the Container div's CSS class to .Progress.
        $get('InvoiceManagerContainer').className = 'Progress';

        // Get a reference to the element that raised the postback,
        //   and disables it.
        $get(args._postBackElement.id).disabled = true;
    }

    // Executed when the async postback completes.
    function EndRequest(sender, args) {
        if (sender._postBackSettings.sourceElement.id !== '') {
            // Change the Container div's class back to .Normal.
            $get('InvoiceManagerContainer').className = 'Normal';
            // Get a reference to the element that raised the postback
            //   which is completing, and enable it.
            $get(sender._postBackSettings.sourceElement.id).disabled = false;
        }
    }

function setHelpContext(sUrl) {
        var ctrl = window.parent.document.getElementById('NonModalHelpContext');
        ctrl.addEventListener("click", window.open("HelpPage.aspx?" + sUrl, "HelpPage", "width=500,height=300,toolbar=no,scrollbars=yes,resizable=no,dependent=yes", false), false);
    }

         
</script>


Any help is appreciated.
Posted
Updated 13-Nov-14 9:07am
v2

1 solution

Try and put the function Out of the document ready Or for JavaScript Window.load .
What happens here is the Dom element loads first and the function is present as the script loaded, so when element would be clicked function gets called.
I hope this helps.
Post back your queries if any.
Thanks.
 
Share this answer
 
Comments
wrappingduke 12-Nov-14 23:32pm    
Hi Suraj,

Thank you for your suggestion. It is appreciated. However, I am not sure I follow what your suggesting. Can you provide an example?

Your help is appreciated.
[no name] 13-Nov-14 2:05am    
What I meant here is, when you load your page, the scripts gets loaded and everything inside the window.load or the document.ready gets loaded on the page load itself. So if the function you have written inside the window.load then it would not work out. So check and put that function outside if it is the case.
Thanks
wrappingduke 13-Nov-14 12:36pm    
Hi Suraj,

Thanks for the reply. I appreciate it. I've tried placing the JavaScript function in two different places:

1. Before the <html> tag, which was the original location of the function. For example,

<script>
function setHelpContext(sUrl){
....
}
</script>

<html>
<head>

2. After the </html> tag, in which case the function can't be found when called from code-behind. For instance,

</html>
<script>
function setHelpContext(sUrl) {
.....
}
</script>


I've tried invoking the function from the Page_LoadComplete and Page_PreRender events, and the results were the function executes when it is located before the <html> tag and the function can't be found when located after </html> tag.


Help is appreciated.
[no name] 13-Nov-14 12:47pm    
Did you check in the console for the error??
is it coming as setHelpContext() not found>?
wrappingduke 13-Nov-14 14:24pm    
Hi Suraj,

Yep, here's the error "JavaScript runtime error: 'setHelpTopic' is undefined". Error occurs when function follows </html> tag.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900