0

So I have a system using jQuery AJAX to load the content dynamically. I have some pages, and they may have a script only for that page (in the js/ folder).

For example, if I'm at the index page and click a post (article.php), the post will dynamically get on the page removing unnecessary content and will get the 'js/article.js' on the page. However, the problem is that if I click on another post after I have visited another, the 'js/article.js' script won't work.

Is there any way i can execute a script that has already been executed on the page, so the problem will be easily solved. If not, do you have any ideas I can fix the problem described?

0

3 Answers 3

1

Did you try http://api.jquery.com/jQuery.getScript/?

0

I'm guessing you need to attach the click events to the newly added content.

jQuery can do that automatically for you if you use .live() or .on() event handlers instead of the classic ones like for example .click().

1
  • Yes, I do this for changing the content but it doesn't help me with scripts.
    – nvlbg
    Mar 24, 2012 at 16:45
0

All scripts and style declarations will be removed from your loaded content (since jquery 1.4, if I remember well)

As jeroen said, you can use .live() or .on() If your loaded content contains specific scripts and css, they will have to be in separate files that you will have to load (as Stefan said) The urls for loading scripts could be known through a naming convention, or you can hide it in your loaded content as an invisible

<a href="..." class="yournamingconventionclassfordynamicscriptsurls"  style="display:none"></a> 

tag, then parse your loaded content and load the referenced files.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.