locked
how to call a javascript function in a sharepoint 2010 webpart? RRS feed

  • Question

  • I made a webpart for SP 2010 using VS 2010.

    I want to call a javascript function using code and not on a onclick event.

    On the page load function, I have this line

    ScriptLink.RegisterScriptAfterUI(this.Page, "/_layouts/myScript.js", false, true);

    And that loads my javascript file which has

    function button_click(str) {
        alert(str);
    }
    //button_click();

    So now how do I call button_click function and pass a parameter to it?

    Thanks

    Thursday, August 16, 2012 7:01 PM

Answers

  • You can register this as a start up script of the Page and invoke it in the page_load

      protected void Page_Load(object sender, EventArgs e)
        {       
            const string someScript = "alertMe";
            if (!ClientScript.IsStartupScriptRegistered(this.GetType(), someScript))
            {
                ClientScript.RegisterStartupScript(this.GetType(),
                    someScript, "alert('I was called from Content page!')", true);
            }

        }

    This way the script gets invoked not only on the button_click event


    Please mark the replies as answers if they help or unmark if not.

    • Proposed as answer by Hiren.j.Patel Friday, August 17, 2012 4:12 AM
    • Marked as answer by Qiao Wei Thursday, August 23, 2012 9:39 AM
    Friday, August 17, 2012 2:57 AM
  • Hello,

    i have created one sample for calling the javascript

    here id my code in .ascx

    <a onclick="OnClickExample();" id="lnkPrint" runat="server">Click</a>

    i have put the javascript file in _layouts folder at below path

    "/_layouts/Sharepoint.Custom/JS/SharePointCustomScript.js"

    i have added two function in .js file

    function OnLoadExample() {
    	alert('Fired on Page Load');
    }
    function OnClickExample() {
    	alert('Fired on Page Load');
    }

    Now i am going to call this two function first when the page is loading and second when click on link means on OnClick event

    so in my webpart load event i have registered the .js file and function

    protected void Page_Load(object sender, EventArgs e)
    		{
    			RegisterJS();
                    }
    
    public void RegisterJS()
    		{
    //Onclick event can also be registed for the server side control			//lnkPrint.Attributes.Add("onclick", "OnClickExample();");
    			string script = @"<script type='text/javascript' charset='utf-8'>
    												_spBodyOnLoadFunctionNames.push('OnLoadExample');
    											</script>";
    			ScriptLink.RegisterScriptAfterUI(this.Page, "/_layouts/Sharepoint.Custom/JS/SharePointCustomScript.js", false, true);
    			Page.ClientScript.RegisterStartupScript(typeof(string), "script", script);
    		}

    _spBodyOnLoadFunctionNames.push('OnLoadExample') function will call the javascript on page Load event,

    i gave two example to call the onclick function .

    1)From the html it self

    2)From the code behind by attribute.add method

    Hope this will clear your idea


    Hiren Patel | Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.

    • Marked as answer by Qiao Wei Thursday, August 23, 2012 9:39 AM
    Friday, August 17, 2012 4:53 AM

All replies

  • You can register this as a start up script of the Page and invoke it in the page_load

      protected void Page_Load(object sender, EventArgs e)
        {       
            const string someScript = "alertMe";
            if (!ClientScript.IsStartupScriptRegistered(this.GetType(), someScript))
            {
                ClientScript.RegisterStartupScript(this.GetType(),
                    someScript, "alert('I was called from Content page!')", true);
            }

        }

    This way the script gets invoked not only on the button_click event


    Please mark the replies as answers if they help or unmark if not.

    • Proposed as answer by Hiren.j.Patel Friday, August 17, 2012 4:12 AM
    • Marked as answer by Qiao Wei Thursday, August 23, 2012 9:39 AM
    Friday, August 17, 2012 2:57 AM
  • Hello,

    i have created one sample for calling the javascript

    here id my code in .ascx

    <a onclick="OnClickExample();" id="lnkPrint" runat="server">Click</a>

    i have put the javascript file in _layouts folder at below path

    "/_layouts/Sharepoint.Custom/JS/SharePointCustomScript.js"

    i have added two function in .js file

    function OnLoadExample() {
    	alert('Fired on Page Load');
    }
    function OnClickExample() {
    	alert('Fired on Page Load');
    }

    Now i am going to call this two function first when the page is loading and second when click on link means on OnClick event

    so in my webpart load event i have registered the .js file and function

    protected void Page_Load(object sender, EventArgs e)
    		{
    			RegisterJS();
                    }
    
    public void RegisterJS()
    		{
    //Onclick event can also be registed for the server side control			//lnkPrint.Attributes.Add("onclick", "OnClickExample();");
    			string script = @"<script type='text/javascript' charset='utf-8'>
    												_spBodyOnLoadFunctionNames.push('OnLoadExample');
    											</script>";
    			ScriptLink.RegisterScriptAfterUI(this.Page, "/_layouts/Sharepoint.Custom/JS/SharePointCustomScript.js", false, true);
    			Page.ClientScript.RegisterStartupScript(typeof(string), "script", script);
    		}

    _spBodyOnLoadFunctionNames.push('OnLoadExample') function will call the javascript on page Load event,

    i gave two example to call the onclick function .

    1)From the html it self

    2)From the code behind by attribute.add method

    Hope this will clear your idea


    Hiren Patel | Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.

    • Marked as answer by Qiao Wei Thursday, August 23, 2012 9:39 AM
    Friday, August 17, 2012 4:53 AM