3

I am working on some code that uses jQuery to parse data out of html documents from the web. This decision was made because jQuery, with its awesome ability to select objects on a page, makes it excellent for parsing.

The code works like this (where 'html_string' is the html of a whole web page):

var page = $(html_string);

The problem I am having is that javascript is being evaluated and executed within the html_string as well. This results in new threads being formed that in some cases, contain infinite loops that make repeated requests to the server and eventually crash the whole client-side of application (not the server).

Is there a way to somehow prevent the execution of javascript in this situation. In this situation, the execution of javascript is an unwanted side effect.

Thanks so much!

6
  • 6
    This line of code will not execute any scripts. Your problem lies somewhere else. Commented Aug 24, 2011 at 20:20
  • how are you pulling the html? Commented Aug 24, 2011 at 20:21
  • 1
    (Adding to @GolezTrol's comment; you can see this by comparing jsfiddle.net/UbCFc to jsfiddle.net/UbCFc/1) Commented Aug 24, 2011 at 20:23
  • 1
    GolezTrol is right up to a point, and if it in fact is a string, why bother with jQuery selector? Just assign the string as string, var page = html_string; otherwise jQuery will definitely execute selector functions trying to find object that would match string as selector. But there is no return or infin. loop that would crash your page. Commented Aug 24, 2011 at 20:27
  • 1
    @Tumharyyaaden It has its use to JQueryfy the string. It allows you to parse the html and find individual nodes, just as you can in your normal document. Commented Aug 24, 2011 at 20:34

4 Answers 4

2

Here is a crappy little jsfiddle that shows you the js does not run when you load the html_string into $. When you click run you will see an immediate alert 'wtf'. Three seconds later, the html is loaded into $ and the body is updated to say 'moo', you should not see the alert.

http://jsfiddle.net/9BAkE/

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

1 Comment

It doesn't answer the question, but it does show that the question is wrong. :)
1

One way would be to parse the html string befor you wrap it with jQuery.

Something like:

var page = html_string;


//then find the script tag (untested code)
int beginning_of_script = page.indexOf('<script>');
int end_of_script = page.indexOf('</script>');

// remove the script
page = page.remove(beginning_of_script, end_of_script);

Comments

1

You could load this syntax into the browser initially as a comment

<script>
/* var page = $(html_string); */
</script>

and then extract the contents of the comment later. The advantage here is that the browser is not going to parse and execute the comment on page load.

You can also explore using jQuery's .load() function, not sure if that will suit your needs.

Comments

1

If you donot care having one extra element, check this! http://jsfiddle.net/UbCFc/4/

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.