0

I am using jQuery's load() function when getting a page to a div like that:

content.php page is :

<script type="text/javascript">
   $(".content").load("asd.php");
</script>

and asd.php is:

<script type="text/javascript">
   alert("Hello World");
</script>

When load ajax finished alert() message appears 3 times. Actually it must appears only 1 time. So load() function get page 3 times.

How can I get the page a time?

sundowatch
  • 3,012
  • 3
  • 38
  • 66
  • do you have only one ".content" item? everything is looking good. check firebug NET reposes as advised. – vsync May 13 '10 at 11:36

2 Answers2

1

Are you sure content loaded three times? Have you used firebug to watch what is actually transfered?

Gopher
  • 917
  • 6
  • 18
1

You are probably facing the same problem as in this question.

Loading content which includes <script> elements into the page is massively unreliable. jQuery tries to paper over the issues but it doesn't quite succeed.

Best: Don't do it. Keep all your static JavaScript code separate from content loaded with load(), and use the post-loading callback to run any JavaScript code you need to bind to the new page content.

Community
  • 1
  • 1
bobince
  • 528,062
  • 107
  • 651
  • 834
  • You are saying that: don't use ajax. Or I understand wronglyi – sundowatch May 13 '10 at 11:51
  • 1
    You can use AJAX, but don't write anything including ` – bobince May 13 '10 at 11:54
  • hmm, sound good. But according to my project, I must load some pages using AJAX with injecting to the content. – sundowatch May 13 '10 at 11:58
  • 1
    You can load some pages using AJAX to inject their content, but don't include ` – bobince May 13 '10 at 12:02
  • How will I load without – sundowatch May 13 '10 at 12:06
  • 1
    Take ` – bobince May 13 '10 at 12:37