0

im fetching html data from a ajax call, the only problem is when i get the data from the ajax call of a url, the assets that are being loaded in that url are not loaded, for example in the url page there are js and html content being loaded when the page is loaded in the browser.

Is there a way in getting all content from the html page loaded by ajax?

Pedro
  • 1,459
  • 6
  • 22
  • 40

1 Answers1

-1

Its because the browser fetches the data as "text" even if there is a header that set Content-type text/html. So you can try something like this : https://api.jquery.com/jquery.parsehtml/ Of if its possible to make an js object:

{
    html : "<div><br />Some text</div>", 
    scripts: [
        "/scripts/script1.js",
        "/scripts/script2.js",
        "/scripts/script3.js"
    ], 
    css: [
        "/stylesheets/style1.css",
        "/stylesheets/style2.css",
        "/stylesheets/style3.css"
    ]
}

And the you can handle the response to load the scripts:

https://api.jquery.com/jQuery.getScript/

mraiur
  • 136
  • 1
  • 2
  • 6
  • But it doesnt makes a lot of sense, because event if the contentType is set to text, the response should bring the scripts that are loaded in the client side dynamacly. – Pedro Nov 18 '19 at 14:24
  • Well it does make a lot of sense from browser speed optimizations. Think it like this. If each request is handled as a page that the user sees then you have a lot of steps. - Finding scripts tags - Checking if they have src or are inline if they are inline then eval them if not make a reguest to load and parse them and so on. Anyway here is something similar to what you are describing as problem: https://stackoverflow.com/questions/36867712/jquery-is-not-loading-js-file-in-script-tag-of-ajax-request – mraiur Nov 18 '19 at 20:03