12

Or any other tags :)

for eg.

  <head>
    <title>page...</title>
    <script> var a = 'abc'; </script>
    <script src="foo.js" type="text/javascript"></script>
  </head>
  <body>
    ...
    <script src="foo2.js"></script>
  </body>

(this string is a response from a ajax call)

I want to get a array with 3 strings:

  1. <script> var a = 'abc'; </script>
  2. <script src="foo.js" type="text/javascript"></script>
  3. <script src="foo2.js"></script>

How can I do that?

3 Answers 3

7

Define: outerHTML function (taken from here)

jQuery.fn.outerHTML = function(s) {
return (s) ? this.before(s).remove() : jQuery("&lt;p&gt;").append(this.eq(0).clone()).html();
}

Then assume your response is stored in data you can do:

$(data).filter("script").each( function(e) { 
    // do something with $(e).outerHTML()
} );
Sign up to request clarification or add additional context in comments.

3 Comments

in a flatten string like the OPs response is, .find() will not match anything. use .filter() instead.
i love you more then life itself
i have school tomorrow and i needed to finish this code before i could go to sleep
2

Use a regular expression with the pattern <script[^<]*</script>.

5 Comments

Regular expressions to match HTML tags are ridiculously more complex than this. I don't think this is a very good answer. Why bother when all the work has been done for you by the browser already.
@Strelok OK, but the string to be parsed is "a response from a ajax call". Alex did not say that this HTML code will be inserted into the page. If it won't than you cannot use the DOM tree, and techniques based on it, like jQuery...
@kol that's simply not true. You can give a string of HTML to jQuery and it will be automatically turned into correct elements by jQuery. try it yourself.
I ran into a case where I needed to extract a nested template from another template (loaded via ajax) and I can't parse it using jQuery because the template contains images with urls that are invalid (because they contain params, that will be replaced once the template is used). So there are legitimate use cases.
+1 for extracting specific elements, regex would be the most efficient way to go! the alternative seems to be parsing the entire html string into a DOM object (ie: using JQuery).
1

You can try something like:

 function getScriptsAsText() {
   var div = document.createElement('div');
   var scripts = [];
   var scriptNodes = document.getElementsByTagName('script');

   for (var i=0, iLen=scriptNodes.length; i<iLen; i++) {
     div.appendChild(scriptNodes[i].cloneNode(true));
     scripts.push(div.innerHTML);
     div.removeChild(div.firstChild);
   }
   return scripts;
 }

It returns a array of the current script elements as text, including their start and end tags.

You might also try outerHTML, but it's not that widely supported.

1 Comment

for this to work you would need to parse the html string into a document. developer.mozilla.org/en-US/docs/Code_snippets/…

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.