0

I'd like to refire scripts (syntaxhighlighter, processingjs) that were loaded in my index.php main page.

I'd also like to execute scripts on the other side of a .load() request...this seems unlikely as these page requests are also hitting .text(), which, as i understand it, removes scripts. Is this kind of thing always dependent on the script itself or is there something I'm not understanding about page compilation?

To reiterate, i'm looking for a way to refire scripts that have already been loaded, but are no longer affecting ajax-loaded content.

expiredninja
  • 1,397
  • 4
  • 25
  • 45

3 Answers3

1

The only way to fire scripts for sure is to reload the page.

You are not misunderstanding anything about page compilation. As the browser renders html it encounters <script> tags. These it executes in the order that it encounters them.

As you yourself say, it is possible to use javascript to remove script blocks. While this is extremely unusual to the point of me only having heard of it a couple of times before (and calling jQuery.fn.text() definitely does not do that), that alone makes it impossible to reliably re-execute everything.

That being said, because we do not usually want code executing before the browser has finished loading all elements into the page therefore it is a common pattern to run all your code only in response to the document's onload event. That is what your code it is doing when it sets up $(document).ready(function(){...}) or $(function(){...}) (those are both identical). Since that code always run in response to the document's loaded event you can always retrigger it with $(document).ready() or $(document).trigger('ready')

However, test this out thoroughly since lots of libraries assume the event occurs only once.

George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • I should use $(document).trigger('ready') at the end of my .load() request? – expiredninja Apr 18 '12 at 22:11
  • @expiredninja - Whenever it is that you want to try rerunning all the load scripts is when you would trigger it. I don't see the point of triggering after the end of your load handler since that would just cause everything that just happened to happen all over again. Also, that would put everything in an endless loop – George Mauer Apr 19 '12 at 02:50
0

Most libraries will come with a function that "runs" them on the page's contents. You'll just have to write calls to these, put them in some kind of "initialization" function, and call this when required.

Graham Clark
  • 12,886
  • 8
  • 50
  • 82
0

You can modify the loaded scripts so that they can be called from a function. You then call this function when you want to run the scripts' contents.

If you don't know how to go about modifying the script get some developer tools like FireBug for FireFox so that you can examine the script as it is being run.

noahnu
  • 3,479
  • 2
  • 18
  • 40