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?

1
  • do you have only one ".content" item? everything is looking good. check firebug NET reposes as advised. Commented May 13, 2010 at 11:36

2 Answers 2

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.

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

6 Comments

You are saying that: don't use ajax. Or I understand wronglyi
You can use AJAX, but don't write anything including <script> to an element's innerHTML (as jQuery's html() or load() would). Keep scripts out of the content you are using AJAX to inject into the page.
hmm, sound good. But according to my project, I must load some pages using AJAX with injecting to the content.
You can load some pages using AJAX to inject their content, but don't include <script> in that content. It doesn't really make sense to, anyway: scripts are supposed to be run once per main page load, not on each sub-navigation. If the scripts include any logic that depends on not defining the same variable or function twice, or needs to run at document load time (ie. document.write()), there's no way it could work anyway.
Take <script> blocks out of the asd.php that you are loading, and include any logic inside them in scripts loaded by content.php. Pass a completed-callback function() { // do asd stuff } to the load() method.
|
1

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

1 Comment

When I open firebug to watch it, it(alert() message) appears a time. But another times it appears 3 times.

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.