gpt4 book ai didi

加载页面时 Javascript 函数不起作用

转载 作者:行者123 更新时间:2023-11-30 12:07:42 24 4
gpt4 key购买 nike

我不太熟悉 JavaScript,但在 .htm 文件中有以下代码。该函数似乎没有在应该被调用的时候被调用;它应该在直接打开页面时调用,即

www.site.com/12345.htm 

将 URL 更改为

site.com/#12345.htm

并加载相关的脚本和css。

除非直接打开,否则不应调用它。即访问 www.site.com/不应该调用它,因为 index.htm 已经立即加载了 css/脚本

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title> Introduction </title>
<script type="text/javascript">
function isTOCLoaded() {
//this function for loading the topic within the application if loaded by itself
//i.e. open the topic .htm file or as search engines link
var url = window.location.href;
var baseUrl = url.substring(0, url.lastIndexOf("/") + 1);
var topicFile = url.substring(url.lastIndexOf("/") + 1)
var newUrl = baseUrl + "#" + topicFile;
location.href = newUrl;
return false;
}
</script>
</head>
<body>
<h1 id="t57117" class="heading1">Introduction</h1>

<input id="topicId" type="hidden" value="57117" />
<input id="topicDescription" type="hidden" value="Introduction" />
<input id="footer-modified" type="hidden" value="Last modified: 17/12/2015 8:18:59 a.m." />
</body>
</html>

最佳答案

如果你想让它被评估,你必须调用你的函数:

<title> Introduction </title>
<script type="text/javascript">
function isTOCLoaded() {
//this function for loading the topic within the application if loaded by itself
//i.e. open the topic .htm file or as search engines link
var url = window.location.href;
var baseUrl = url.substring(0, url.lastIndexOf("/") + 1);
var topicFile = url.substring(url.lastIndexOf("/") + 1)
var newUrl = baseUrl + "#" + topicFile;
location.href = newUrl;
return false;
}

// ADDED: run the function
isTOCLoaded();
</script>
</head>

在 JavaScript 中,function 关键字用于声明 一个供以后使用的函数,直到第一次调用它时才真正运行函数的内容.

关于加载页面时 Javascript 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34709984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com