0

I am developing a site so that it can be accessed offline. I've tried and it worked. But there is a little problem, my content is not structured properly.

Save as myjs.js

var text = '<html> <head> <title> TEST BRO </title> </head> <body> HELLO WORLD </body> </html>';
document.write (text);

On another page, I accessed the script above. Save as page.html

<script src="myjs.js"></script>

I enable Cache in .htaccess to make the content accessible offline. However, in the inspect element, I see that the HEAD tag is empty, and the TITLE tag is inside BODY. How to solve this problem?

I only need JavaScript, not jQuery. Please help me

Prabowo
  • 1
  • 1
  • 1
    Why `document.write()`? – Andreas Nov 18 '20 at 07:59
  • @Andreas I want to show the articles that have been stored in the cache, is there any other solution besides document.write? – Prabowo Nov 18 '20 at 08:01
  • Yes, the other solution is to save *the article*, not the full HTML. Afterwords, just place it in the body of the document in a specific container. (e.g, `
    `). Then you can preserve your layout as it is or even change it but the article text would just show up where it should be.
    – VLAZ Nov 18 '20 at 08:10
  • @VLAZ But it doesn't work for my project, it can't be accessed Offline – Prabowo Nov 18 '20 at 08:19
  • What do you mean? Using `document.write(text)` is exactly the same action as `document.querySelector("#article").textContent = text` in terms of "offline". If you can do one, you can do the other. – VLAZ Nov 18 '20 at 08:23
  • @VLAZ first, visit `page.html` – Prabowo Nov 18 '20 at 08:28
  • Next, visit `data:text/html;base64,BLABLABLA...` its can accesed Offline – Prabowo Nov 18 '20 at 08:28
  • `BLABLABLA` = `` after convert to `base64` – Prabowo Nov 18 '20 at 08:30
  • @Prabowo this in no way makes it clear. If you can get `mjs.js` in whatever fashion, then it doesn't matter what the content is - `document.write(text)` or `document.querySelector("#article").textContent = text`. And the two operations are the same in that they don't require specific online access. You can't claim to be able to replace the whole documen but then say that you cannot use DOM manipulation, as *they both use the same API*. No difference there. You're just trying to do *more* work by saving the whole document instead of just the part that should change. – VLAZ Nov 18 '20 at 08:42
  • @VLAZ can you put `` into the `Head` tag using `document.querySelector` ? – Prabowo Nov 18 '20 at 09:03

0 Answers0