更改脚本“src”通过 URL 参数并使其起作用
我在想是否可以在静态网站上执行此操作,
我有 的链接
https://www.example.com?year=2022&week=05
,主要 HTML 是
<h4 style="color:#FFD700"><strong>TITLE</strong><br></h1>
<h3 id="1testing"></h3>
<h6 id="2testing"></h6>
// skipped
<script id="results" src="path/to/script/202205.txt"></script>
现在,我想让它根据 URL 参数进行更新,我有一个 URL 参数(并更新上面的代码),
const queryString = window.location.search;
console.log(queryString);
const urlParams = new URLSearchParams(queryString);
const yrs = urlParams.get('year')
const wks = urlParams.get('week')
console.log(yrs);
console.log(wks);
document.getElementById('results').src = "path/to/script/" + yrs + "/" + wks + ".txt"
txt 文件的格式是
document.getElementById("date").innerHTML =// date
"date";
// Number 1
document.getElementById("1testing").innerHTML =// 1testing
"1testing";
document.getElementById("2testing").innerHTML =// 2testing
"2testing";
//so on...
但是,我遇到的麻烦是,src 标签在加载后更新,内容根本没有更新,可能是因为脚本不按顺序,但是我对此了解不多,任何人都可以帮助解决这个问题?
I am thinking if it's possible for doing this on a static website,
I have the link of
https://www.example.com?year=2022&week=05
and the main HTML is
<h4 style="color:#FFD700"><strong>TITLE</strong><br></h1>
<h3 id="1testing"></h3>
<h6 id="2testing"></h6>
// skipped
<script id="results" src="path/to/script/202205.txt"></script>
Now, I wanna make it to update according to the URL Param, I've got a URL Param (and update the code above) by
const queryString = window.location.search;
console.log(queryString);
const urlParams = new URLSearchParams(queryString);
const yrs = urlParams.get('year')
const wks = urlParams.get('week')
console.log(yrs);
console.log(wks);
document.getElementById('results').src = "path/to/script/" + yrs + "/" + wks + ".txt"
The txt files are in the format of
document.getElementById("date").innerHTML =// date
"date";
// Number 1
document.getElementById("1testing").innerHTML =// 1testing
"1testing";
document.getElementById("2testing").innerHTML =// 2testing
"2testing";
//so on...
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您应该删除脚本标签并重新添加更新的标签。
I think you should remove the script tag and re-add the updated one.