-1

How can I display the HTML content of a XHR responseText in a DIV and strip out its <script> tags and append them to the head tag? (using Google Closure)

Something like extractScripts in Prototype JS.

ehsun7b
  • 4,796
  • 14
  • 59
  • 98

1 Answers1

0

If the responseText is a whole web page with HTML and script tags, then maybe you should just attach it to an iFrame and the iFrame src and let the browser manage it all for you rather than put it in a DIV where you have to manage the different parts of the web page.

If you really want to get the responseText and put it directly into a div in your own document, then you have a couple options.

First, if the response is just an HTML fragment with embedded script tags (not a whole web page), then you can just read the whole response into a JS variable and then assign it to the .innerHTML attribute of your DIV. The browser will parse the HTML, including the embedded tags and evaluate it as if it was originally part of that div (except the script execution timing obviously isn't until right after you assign the .innerHTML.

As an example:

var myDiv = document.getElementById("myDiv");
myDiv.innerHTML = responseText;

Second, if there's some reason you want to pull out the tags separately (and I can't think of such a reason versus the first option), then you'll have to parse the start and end script tags out of the responseText yourself, put the JS code text in between them into a JS variable and use eval on that variable to execute it and make it part of your document. Then, assign the remaining HTML to the innerHTML attribute of your div as in the example above.

If that script has document.write in it, that won't work this second way because there's no location context for the document.write. If those script tags rely on any page loading methods like onload, then the scripts also won't see those events (except if it's the src of an iFrame.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thanks but I asked for a solution based on Google Closure. Secondly, innerHTML is not a standard and also I strongly need to strip out the – ehsun7b Jun 30 '11 at 09:23
  • Can you explain what you mean by "a solution based on Google Closure"? Closure is a set of tools and a library. Are you asking for someone to write code for you using the Closure library? Or are you asking conceptually how to do this? I've given you three conceptual options so far. – jfriend00 Jun 30 '11 at 09:33
  • Why do they need to be added to the head tag? If you just parse out the script between the – jfriend00 Jun 30 '11 at 09:35
  • innerHTML is something I'm aware of, but none of these solution strips script tags and add theme to the head. and the first solution (iframe) is not actually adding the new html to the DOM of current document. Adding script tags to the head means that let those script to be executed and be used later on. – ehsun7b Jun 30 '11 at 10:14
  • Parsing the script out and doing an eval on it gives you the same results as putting it in a head tag. The script gets executed in the context of your current page and all functions or variables on the script are active just the same as if in the head tag. I'm not understanding what that doesn't work. Quite frankly, I don't know how to put something in the head tag without using a src=url, but I do know you could do an eval on the parsed code and get the same effect. – jfriend00 Jun 30 '11 at 10:22
  • no it is not exactly the same. For example defining functions and variables are different. actually I'm looking for something like this in Google Closure: http://www.prototypejs.org/api/string/stripScripts – ehsun7b Jun 30 '11 at 10:37
  • Then, why don't you clarify your question to say what you're really looking for. – jfriend00 Jun 30 '11 at 11:06
  • I thought my question is clear enough for javascript developers, sorry. Most of the experienced JS developers know that eval and adding the JS tags to the DOM are not exactly the same. and many other aspects of my question. Again I'm sorry if my question misleaded you. – ehsun7b Jun 30 '11 at 14:40