locked
How to call javascript Function before page load RRS feed

  • Question

  • Hi
    I am trying to get screen resolution through javascript by calling the javascript method on page load. and inside javascript i am assigning resolution value in the hidden control.

    Page Load:

    protected void Page_Load(object sender, EventArgs e)
    {
    string strMethodName = "GetScreenHeightAndWidth();";
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(), strMethodName, true);
    }

    Js:

    function GetScreenHeightAndWidth() {
            var width = screen.width;
            var height = screen.height;
            var object = '<%=ScreenReslnHeightHideenField.ClientID%>'
            document.getElementById(object).value = height;
        }

    after this js call i'm trying to get value from hidden control which i assigned through javascript. but i'm getting empty value.

    at the same time after the page load is done, when i try to postback by click any of the button, i'm able to get the value from hidden control. this means after page load is done javascript method is calling.

    so, how to get the value in page load itself. based on this value i need to assign some styling in my web page.

    Regards,
    Ashok

    Friday, November 13, 2009 11:11 AM

All replies

  • why dont u call this in body onload event ??  so after the page gets render it will pick up the value from the control...

    hope this helps u


    Narayanan Dayalan - Zeetaa Business Solutions Please Mark "Propose As Answer", if my answer works well with ur Query
    Friday, November 13, 2009 1:22 PM
  • Hi Narayanan

    Thanks for your reply. I already tried this script in body onload event also. but it was not helpful.

    Regards,
    M.seenuvasan
    Monday, November 16, 2009 5:03 AM

  • function GetScreenHeightAndWidth() { //delete this line
            var width = screen.width;
            var height = screen.height;
            var object = '<%=ScreenReslnHeightHideenField.ClientID%>'
            document.getElementById(object).value = height;
        } //and this!

    [place the code right after the element to which "object" reference will be pointing to]
    no event handler will be needed!!
    Tuesday, November 24, 2009 8:07 PM