【问题标题】:Replace window when script ends JavaScript/HTML [duplicate]脚本结束 JavaScript/HTML 时替换窗口 [重复]
【发布时间】:2021-07-14 03:49:09
【问题描述】:

有没有办法插入:

location.replace("https://www.google.com");

在我的 javascript 中的所有函数都运行之后,也就是我的脚本何时结束?我尝试将它放在脚本的不同部分,但它只是立即替换它。 这是我目前的脚本(对不起,它是一个文字冒险游戏太长了):

var currentFrame = 0;
var images = document.getElementById("images"); 
var text = document.getElementById("text"); 
var buttonBox = document.getElementById('buttonBox');
var input = document.getElementById('input');
//this is the variable for the name of the character
var yerdog;



//this is how after we type in the character name and hit enter
//we will add the name to the variable, remove the input box and start our first scenario
input.onkeypress = function(event) {
  console.log(input.value);
  if (event.key == "Enter" || event.keyCode == 13) {
    yerdog =  input.value;
    input.parentNode.removeChild(input)
    advanceTo(scenario.two)
  }
};


//this changes the text and puts in your characters name
var changeText = function(words) {
  text.innerHTML = words.replace("Your dog", yerdog);
};

//this takes the image link and puts it in the proper format, sending it to the html
var changeImage = function(img) {
  images.style.backgroundImage = "url(" + img + ")";
};


//this looks at the number of options we have set and creates enough buttons 
var changeButtons = function(buttonList) {
  buttonBox.innerHTML = "";
  for (var i = 0; i < buttonList.length; i++) {
    buttonBox.innerHTML += "<button onClick="+buttonList[i][1]+">" + buttonList[i][0] + "</button>";
  };
};

//this is what moves the game along
var advanceTo = function() {
  if (currentFrame >= scenario.length) {
    //finish the game code here:
    location.replace("https://www.google.com");
  }
  
  var s = scenario[currentFrame];
  changeImage(s.image)
  changeText(s.text)
  changeButtons(s.buttons)
};


var scenario = [
  one: {
    image: "https://s9.postimg.org/eceo9mp73/5860028206_d66810105f_b.jpg", //dog
    text: "You have finally decided to take your dog out for a walk. You smile and pat your trusty companion's head. What the dog's name?\n",
  },

  
];
input.onkeypress = function(event) {
  console.log(input.value);
  if (event.key == "Enter" || event.keyCode == 13) {
    yerdog =  input.value;
    input.parentNode.removeChild(input)
    currentFrame = currentFrame + 1;  // <--- here
    advanceTo() // <-- and here
  }
};

//this is the code that starts the game
advanceTo(scenario.one);

【问题讨论】:

  • 请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange 网络上发帖,您已在 CC BY-SA 4.0 license 下授予 Stack Exchange 分发该内容的不可撤销的权利(即无论您未来的选择如何)。根据 Stack Exchange 政策,帖子的非破坏版本是分发的版本。因此,任何破坏行为都将被撤销。如果您想了解更多关于删除帖子的信息,请参阅:How does deleting work?
  • 它不会让我删除它,因为人们已经回答了它。
  • @samson24 花点时间阅读meta.stackoverflow.com/a/403803
  • 不要使用script 标签!每个 SO 问题都是关于脚本的一种或另一种方式

标签: javascript html function replace


【解决方案1】:

var currentFrame = 0;
var images = document.getElementById("images"); 
var text = document.getElementById("text"); 
var buttonBox = document.getElementById('buttonBox');
var input = document.getElementById('input');
//this is the variable for the name of the character
var yerdog;


var scenario = [
{
    image: "https://s9.postimg.org/eceo9mp73/5860028206_d66810105f_b.jpg", //dog
    text: "You have finally decided to take your dog out for a walk. You smile and pat your trusty companion's head. What the dog's name?\n",
    buttons: [],
  },
]


//this changes the text and puts in your characters name
var changeText = function(words) {
  text.innerHTML = words.replace("Your dog", yerdog);
};

//this takes the image link and puts it in the proper format, sending it to the html
var changeImage = function(img) {
  images.style.backgroundImage = "url(" + img + ")";
};


//this looks at the number of options we have set and creates enough buttons 
var changeButtons = function(buttonList) {
  buttonBox.innerHTML = "";
  for (var i = 0; i < buttonList.length; i++) {
    buttonBox.innerHTML += "<button onClick="+buttonList[i][1]+">" + buttonList[i][0] + "</button>";
  }
};

//this is what moves the game along
var advanceTo = function() {
  if (currentFrame >= scenario.length) {
    //finish the game code here:
    console.log("end of the game");
    //location.replace("https://www.google.com"); // uncomment on your page
    return;
  }
  var s = scenario[currentFrame];
  changeImage(s.image)
  changeText(s.text)
  changeButtons(s.buttons)
};



input.onkeypress = function(event) {
  console.log(input.value);
  if (event.key == "Enter" || event.keyCode == 13) {
    yerdog =  input.value;
    input.parentNode.removeChild(input)
    currentFrame = currentFrame + 1;  // <--- here
    advanceTo() // <-- and here
  }
};

//this is the code that starts the game
advanceTo();
<div id="images"></div>
<div id="text"></div>
<div id="buttonBox"></div>
<input type="text" id="input"/>

根据您发布的代码,您有 3 个重要部分:

  • advanceTo 函数
  • input.onkeypress 事件
  • 场景列表

为此,我可以建议以下两种更改之一:

  • 将场景更改为数组(当没有更多元素时结束并调用replace
  • 添加一个end 场景(调用replace

数组方法:

您可以将场景更改为数组而不是对象:

var scenario = [
  {
    image: "https://s9.postimg.org/eceo9mp73/5860028206_d66810105f_b.jpg", //dog
    text: "You have finally decided to take your dog out for a walk. You smile and pat your trusty companion's head. What the dog's name?\n",
  },
  {
    image: "https://s9.postimg.org/eceo9mp73/5860028206_d66810105f_b.jpg", //dog
    text: "You have finally decided to take your dog out for a walk. You smile and pat your trusty companion's head. What the dog's name?\n",
  },
]

还将当前帧存储在一个变量中(我建议在您的代码顶部):

var currentFrame = 0;

所以你可以调用AdvanceTo并开始游戏(我们不再传递索引,因为它存储在currentFrame变量中:

advanceTo()

把进阶的逻辑稍微改一下:

var advanceTo = function() {
  if (currentFrame >= scenario.length) {
    //finish the game code here:
    location.replace("https://www.google.com");
    return;
  }
  
  var s = scenario[currentFrame];
  changeImage(s.image)
  changeText(s.text)
  changeButtons(s.buttons)
};

最后,你需要改变你的input.onkeypress事件:

input.onkeypress = function(event) {
  console.log(input.value);
  if (event.key == "Enter" || event.keyCode == 13) {
    yerdog =  input.value;
    input.parentNode.removeChild(input)
    currentFrame = currentFrame + 1;  // <--- here
    advanceTo() // <-- and here
  }
};

【讨论】:

    【解决方案2】:

    你可以使用 ifelse 语句以及它的超时时间

    setTimeout ( "location()", 5000 );
    
    function location( )
    {
      location.replace("https://www.google.com");  
    }
    

    【讨论】:

    • 这对我不起作用,它破坏了我的 js 的其余部分:/
    • 你把它放在哪里了?您已经添加了有问题的代码,但是您在哪里设置 location.replace 您必须具有 main 功能。
    • 我把它放在脚本的开头,但它破坏了我的其余代码
    • 制作 main 函数并在其中创建 ifelse 条件,代码在最后成功运行的地方粘贴 setTimeout 和 location.replace。
    • @Tayyabmehar 你有错别字 1.location 是保留字 2.你没有在setTimeout 中调用location(),它只是一个字符串,什么都不做。这不是 OP 问题的准确解决方案
    猜你喜欢
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 2016-11-06
    • 2012-09-01
    • 2017-03-12
    • 2022-01-18
    相关资源
    最近更新 更多