0

I'm trying to re-code our app into a single-page application.

For that I have written a php that does some access verification and then delivers the requested site's content via include as an AJAX response.

This works, except for the js files.

A requested site's script tag looks like this:

<script src="/javascript/statistics.js"></script>

Which worked back when the site was not delivered via AJAX. But now it seems that '/' doesn't refer to the site root anymore? What's going on here? I'm not getting any errors either, the .js is just not included at all.

For reference, this very same-looking css included in the same file works fine and shows up as a GET request in console:

<link rel="stylesheet" type="text/css" href="/CSS/statistics.css" />

whereas the js isn't shown as requested in console.

MiK
  • 918
  • 4
  • 16
  • when you inspect the console are there errors? Does the javascript file load or not? Do Ajax requests get sent or not? – Professor Abronsius Nov 11 '19 at 09:33
  • Ajax request is sent - page code is returned - js file referenced in page code isn't getting included - if I try to use a function in the referenced .js, it says it's not defined. There's no GET for the .js file in console either. – MiK Nov 11 '19 at 09:34
  • how do you do your ajax, with jquery,vanilla, or axios? – Matius Nugroho Aryanto Nov 11 '19 at 09:38
  • Vanilla Javascript. – MiK Nov 11 '19 at 09:39
  • so you send the ajax request which returns an html page with the ( or supposed to be with ) the javascript and css. When you say `it works except for js files` ~ do you mean that the javascript files do not execute or they are simply not in the response data at all? – Professor Abronsius Nov 11 '19 at 09:44
  • They appear in the AJAX response as `````` - and the CSS adds a row to the dev console (network tab) with GET and Code 200, but the JS doesn't add a row, it's not even getting loaded. – MiK Nov 11 '19 at 09:46
  • the javascript is not being included in the normal page flow so will not execute like you are hoping it will. I have come across this before - trying to remember the solution – Professor Abronsius Nov 11 '19 at 09:49
  • Maybe I have to include every .js file from every subpage in the wrapper page that will request the specific sub-sites via AJAX? A bit impractical to transmit all js files at once, but then afterwards everything would be cached anyways... – MiK Nov 11 '19 at 09:54
  • Kind of. Seems that there is no easy way to include js files dynamically, other than using eval() which I'm hesitant to. I'll have to completely rewrite all sites and then either include one big .js or a collection of small .js files in the wrapper of the app. – MiK Nov 11 '19 at 10:12
  • obviously we have not seen the ajax function nor the site structure but you could, perhaps, process the HTML document in PHP using DOMDocument and DOMXPath to find all the javascript files / snippets and send them along with the response so the ajax callback can process them separately – Professor Abronsius Nov 11 '19 at 10:21

0 Answers0