Click here to Skip to main content
15,889,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all I have this function

JavaScript
$(document).ready(function () {
       $("#EnterLongUrl").click(function () {
           $.ajax({
               type: "POST",
               url: "/home/GetShortURL",
               contentType: "application/json;charset=utf-8",
               dataType: "json",
               data: '{ "longUrl" : "' + $("#LongUrlText").val() + '" }',
               success: function (results) {
                   $("#ShortUrl").val(results);
               },
               error: function (err) {
                   alert(err.status + " - " + err.statusText);
               }
           });
       });
   });


I need to fire this function in page load instead of clicking any button .

What I have tried:

I don't want what I must tried to do that help me pleas .
Posted
Updated 8-May-16 19:14pm

You should read the jQuery, click documentation[^]...
Quote:
Description: Bind an event handler to the "click" JavaScript event, or trigger that event on an element.

So for your case:
JavaScript
$("#EnterLongUrl").click();
 
Share this answer
 
try this

HTML
<script>
        $(document).ready(function () {
            MyFunction();
            $("#EnterLongUrl").click(function () {
                MyFunction();
            });
        });

        function MyFunction()
        {
            $.ajax({
                type: "POST",
                url: "/home/GetShortURL",
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                data: '{ "longUrl" : "' + $("#LongUrlText").val() + '" }',
                success: function (results) {
                    $("#ShortUrl").val(results);
                },
                error: function (err) {
                    alert(err.status + " - " + err.statusText);
                }
            });
        }


    </script>
 
Share this answer
 
Comments
Sarah Mohammed77 9-May-16 11:44am    
thank you so much it works
Karthik_Mahalingam 9-May-16 11:46am    
Welcome sara
try this

it will forcefully fire the click event on pageload

JavaScript
$("#EnterLongUrl").trigger('click');
 
Share this answer
 
Comments
Sarah Mohammed77 9-May-16 11:44am    
it works thank you so much
Hafiz Saad Ullah 11-May-16 6:54am    
Most Welcome :)

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