I am thinking if it’s possible for doing this on a static website,
I have the link of
JavaScript
x
2
1
https://www.example.com?year=2022&week=05
2
and the main HTML is
JavaScript
1
8
1
<h4 style="color:#FFD700"><strong>TITLE</strong><br></h1>
2
<h3 id="1testing"></h3>
3
<h6 id="2testing"></h6>
4
5
// skipped
6
7
<script id="results" src="path/to/script/202205.txt"></script>
8
Now, I wanna make it to update according to the URL Param, I’ve got a URL Param (and update the code above) by
JavaScript
1
9
1
const queryString = window.location.search;
2
console.log(queryString);
3
const urlParams = new URLSearchParams(queryString);
4
const yrs = urlParams.get('year')
5
const wks = urlParams.get('week')
6
console.log(yrs);
7
console.log(wks);
8
document.getElementById('results').src = "path/to/script/" + yrs + "/" + wks + ".txt"
9
The txt files are in the format of
JavaScript
1
11
11
1
document.getElementById("date").innerHTML =// date
2
"date";
3
4
// Number 1
5
document.getElementById("1testing").innerHTML =// 1testing
6
"1testing";
7
document.getElementById("2testing").innerHTML =// 2testing
8
"2testing";
9
10
//so on...
11
However, the trouble I’m having is that, the src tag updated AFTER loading and the content is simply not updating, possibly because of the scripts not in order, however I’m not having much knowledge on this, anyone could help with a solution on this?
Advertisement
Answer
I think you should remove the script tag and re-add the updated one.
JavaScript
1
13
13
1
let oldScriptTag = document.getElementById('results');
2
3
let newScriptTag = document.createElement('script');
4
newScriptTag.id = 'results';
5
newScriptTag.src = 'path/to/script/' + yrs + '/' + wks + '.js';
6
newScriptTag.textContent = '//script';
7
8
var body = document.getElementsByTagName('body')[0];
9
10
oldScriptTag.remove();
11
12
body.appendChild(newScriptTag);
13