0
<script type="text/javascript" >

        function getDetails() {
            var IDex = getQueryStringVariableByName("GameID");

            $.ajax({
                type: "POST",
                url: "http:/...",
                data: "{'ItemID': '" + escape(IDex) + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    var data = response.d;
                    $('#output').empty();
                    $.each(data, function (index, item) {
                        var str = "Title: " + item.Name + "<br /> Current Price: " + item.CurrentPrice + "<br /> Developer: " + item.Developer + "<br /> Genres: " + item.Genre
                        $('#output').append('<li>' + str + '</li>');
                    });
                },
                failure: function (msg) {
                    $('#output').text(msg);
                }
            });
        }


         function getQueryStringVariableByName(name) {
             //use this function by passing it the name of the variable in the query
             //string your are looking for.  For example, if I had the query string
             //"...?id=1" then I could pass the name "id" to this procedure to retrieve
             //the value of the id variable from the querystring, in this case "1".
             name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
             var regexS = "[\\?&]" + name + "=([^&#]*)";
             var regex = new RegExp(regexS);
             var results = regex.exec(window.location.search);
             if (results == null)
                 return "";
             else
                 return decodeURIComponent(results[1].replace(/\+/g, " "));
         }

         window.onload = GetDetails;
         //$(document).ready( function () {
         //    getDetails();
         //
</script>

I have tried multiple methods to get getDetails to run when the page loads. I have tried the window.onload method, putting it in the body tag, and a few others but I can't seem to find a way to get it to load automatically.

2
  • 3
    i.e because GetDetails != getDetails It is case sensitive Commented Dec 3, 2013 at 3:02
  • are you sure that jquery is linked as a script ? Commented Dec 3, 2013 at 3:13

5 Answers 5

1

To check if the compiler was actually reading your code when the page loads is

$(document).ready(function() { 
    getDetails();
});

function getDetails() { 
   alert("this is a try");
   //or
   console.log("this is a try");
}

and of course include the jquery framework to your document like

<script src = "path" type = "text/javascript"></script>

The best way to do is write it before you have to close the body tag

Hope it helps!

Sign up to request clarification or add additional context in comments.

2 Comments

The alert is working, so the compiler is working at least. Must be another error.
Did you check what @PSL had commented?
0

I'm not sure I understand the question, but the way to run a function a after page loads in jQuery is this:

$(document).ready(function() {

    // Your code goes here..
    myFunction();

});

Comments

0

It doesn't work?

$(function(){
         getDetails();
    });

Comments

0

it should be getDetails or you could use:

$(document).ready(function(){
  functionname();
});

Comments

0
$(document).ready(function() {
   getDetails();
});

will work for sure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.