参数化 func() 选择<body onload= "func()" > 标记,具体取决于 HTML 中的参数参数



我有一个运行菜单的index.html

<a href='./satellites.html'> 
<a href='./simulation.html'>

服务器通过发送请求的两个文件中的一个文件和一个标头进行响应。这两个文件(satellites.html和simulation.html(几乎相同,只是加载时调用的函数不同

<!-- in the satellites.html-->
<body onload='init()'>            
...
and 
<!-- in the simulation.html-->
<body onload='initSimulation()'>  
...

否则,这两个HTML程序是相同的。

一切都很好,但我想避免重复相同的代码。服务器如何控制客户端将调用哪个函数(可能使用URL参数(?我搜索了一下,但似乎标题在HTML或javascript中都不可用。

使用cookie提供了一个解决方案,我不喜欢它,因为有些用户不允许使用cookie。不过,我还是把它放在这里作为参考:在index.html(客户端菜单(中:

<!-- in index.html-->
<body>
...
<a href='./satellites.html' class='button'>
<a onclick="return false" ondblclick="startSimulation()" class='button'>
...
<script>
function startSimulation(){
document.cookie = "simulation=true";
window.location.replace("./AISsatellites.html");
}
</script>
</body>

现在,服务器存储和发送的文件总是相同的。客户端通过cookie决定调用哪个函数,如下所示:

<!-- in satellites.html-->   
<body onload='select_Simulation_or_Online()'>
...
<script>
function select_Simulation_or_Online(){
if(document.cookie.indexOf('simulation=')==0) {
simulation = true;
document.cookie = 'simulation=; expires=Thu, 01-Jan-1970 00:00:01 GMT;';
initSimulation();
}
else {
simulation = false;
init();
}
...
</script>
</body>

仍在寻找一个不使用cookie的解决方案——可能使用服务器放置在标头中的参数,由客户端读取。

最新更新