April 26, 2011

JQuery load at display page

Question by cosma

I’m newbie on web technologies and particular on jQuery so my question can be stupid.

What I want is to call a particular file (using .get or .post jQuery) when I click on some link and also load that file into browser. Also, the browser address should point to that file. This is what I have tried:

$('#my_link').click(
    function(event){
        event.preventDefault();
        $.get("index.php", function(data){
            $(document).html(data);
        });
    }
);

P.S.
I’m using .get/.post because I want to send some param. to that page (index.php), param. used to fill some fields.

Answer by Starx

I will suggest you, to try .load() method instead. It also provides options for sending params.

Here is an example based on the question.

$('#my_link').click(function() {
   $('#mydiv').load ("index.php", { param1: val1, param2: val2 }, function) {
      alert('loaded');
   });
});

I would suggest to load the contents on a division rather than the document itself.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!