0

  I've started working on a project that loads all of the different pages content dynamically.
On the surface this seems simple enough, an AJAX call to a script that returns the content that is placed inside a DIV. Except that not only HTML but JS is returned as well. I'm seeing a lot of things like this:-

<img src="spacer.gif" height="1" width="1" onload="SOMEJSHERE"/>

Dotted in the code to execute JS functions. This doesn't provide the kind of HTML/JS code separation I've come to love using JQuery.
  I can understand that they don't want to load all the JS and HTML at once, there is an awful lot of it... But this just doesn't feel like the best way.

Some experience and suggestions please?

Lyle

2
  • i failed to understand why you can't separate the js with that element... Commented Sep 21, 2010 at 1:28
  • What do you mean by "separate the JS with the element"? Commented Sep 21, 2010 at 1:40

3 Answers 3

1

Have a look at the jQuery .live() event. It can apply behaviors (event bindings) to all current and future elements on the page, which match the given selectors.

This means that your newly-loaded HTML need not contain any script. Just make sure the loaded elements have the right selectors (class names and id's).

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

Comments

0

hey i use "eval" to execute js dynamically.

e.g.

<script>
var strjs = 'function execute(){alert("foobaring");} execute();';

eval(strjs);

</script>

1 Comment

yeah, I was thinking I could get the AJAX to return the HTML and JS separately, then just eval the JS... But is that ideal?
0

In the end I used the tabs.select option from JQuery UI (which we were using for the ajaxed pages) and executed the appropriate JS as needed.

Comments

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.