在页面之间发送命令/数值

huangapple go评论69阅读模式
英文:

sending commands/values between pages

问题

我正在制作一个网站,其中有2个页面,第1个页面是接收器,第2个页面是遥控器,基本上您可以在第2页输入文本,一旦单击提交,第1页开始播放来自第2页的文本到语音消息

index.html(又名:page1)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="src/style.css">
  </head>
  <body>
    <h1 id="header"></h1>

    <script src="src/script.js"></script>
  </body>
</html>

control.html(又名:page2)

<body>
<center>
<form>
<h1 style="color:green">Javatpoint</h1>
<h3>Confirm password Validation Example</h3>
<!-- 输入密码 -->
<td>输入密码</td>
<input type="password" name="pswd1"> <br><br>
<button type="submit" onclick="matchPassword()">提交</button>
<script>
var pw1 = document.getElementById("pswd1");
function matchPassword() {
  <script src="script.js"><script> var x1
}
</script>

script.js(page1的脚本)

const message = 'Hello world' // 尝试编辑我

// 更新标题文本
document.querySelector('#header').innerHTML = message

// 在控制台中记录
console.log(message)
var audio = new Audio('notif.mp3');
audio.play();
var msg = new SpeechSynthesisUtterance();
msg.text = "hallo jeremy";
window.speechSynthesis.speak(msg);

我无法找到一种方法将第2页内的文本发送到第1页。

英文:

im working on a website with 2 pages 1 is the receiver and 2 is the remote basicly you can enter a text on page 2 and once you hit submit page1 starts playing a text to speatch message with the text inut from page2

index.html (aka : page1)

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;src/style.css&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1 id=&quot;header&quot;&gt;&lt;/h1&gt;

    &lt;script src=&quot;src/script.js&quot;&gt;&lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

control.html (aka : page2)

&lt;body&gt;
&lt;center&gt;
&lt;form&gt;
&lt;h1 style=&quot;color:green&quot;&gt;Javatpoint&lt;/h1&gt;
&lt;h3&gt; Confirm password Validation Example &lt;/h3&gt;
&lt;!-- Enter Password --&gt;
&lt;td&gt; Enter Password &lt;/td&gt;
&lt;input type = &quot;password&quot; name = &quot;pswd1&quot;&gt; &lt;br&gt;&lt;br&gt;
&lt;button type = &quot;submit&quot; onclick=&quot;matchPassword()&quot;&gt;Submit&lt;/button&gt;
&lt;script&gt;
var pw1 = document.getElementById(&quot;pswd1&quot;);
function matchPassword() {
  &lt;script src=&quot;script.js&quot;&gt;&lt;script&gt; var x1
}
&lt;/script&gt;

script.js of page1

const message = &#39;Hello world&#39; // Try edit me

// Update header text
document.querySelector(&#39;#header&#39;).innerHTML = message

// Log to console
console.log(message)
var audio = new Audio(&#39;notif.mp3&#39;);
audio.play();
var msg = new SpeechSynthesisUtterance();
msg.text = &quot;hallo jeremy&quot;;
window.speechSynthesis.speak(msg);

i cant find a way to send the text inside page2 to page 1

答案1

得分: 1

这里是翻译好的部分:

有许多方法可以实现这一点,但我只会展示一种。你可以通过查询参数轻松在页面之间传递数据,这些参数实质上是附加在URL末尾的数据片段。

为了利用这些参数,在用户在control.html页面按下按钮时,你需要重定向到你的index.html页面。幸运的是,这可以通过为提交按钮添加事件监听器来完成。

以下是代码:

control.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  </head>
  <body>
    <form>
      <p>在此输入内容:</p>
      <input type="text" id="text-input" name="text" />
      <input type="submit" id="submit-button"></input>
    </form>
    <!-- 其余文档... -->
    <script src="src/control.js"></script>
  </body>
</html>

src/script.js

const queryString = window.location.search;
const queryParams = new URLSearchParams(queryString);

const message = queryParams.get("text");

console.log(message);

// 其余文件...

src/control.js

const button = document.getElementById("submit-button");
button.addEventListener("click", handleText);

function handleText(event) {
  event.preventDefault();

  const text = document.getElementById("text-input").value;
  const currentURL = window.location.pathname;
  const currentDir = currentURL.substring(0, currentURL.lastIndexOf("/"));

  window.location.replace(currentDir + "/index.html?text=" + text);
}

希望对你有所帮助!

英文:

There are many ways that you could achieve this, but I'll show you just one. You can easily pass data between pages using query parameters, which are essentially pieces of data appended to the end of a URL.

In order to utilize these, you would need to redirect to your index.html page whenever the user presses the button in the control.html page. Fortunately, this can be done by adding an event listener to your Submit button.

Here is the code below:

control.html

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; /&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form&gt;
      &lt;p&gt;Enter stuff here:&lt;/p&gt;
      &lt;input type=&quot;text&quot; id=&quot;text-input&quot; name=&quot;text&quot; /&gt;
      &lt;input type=&quot;submit&quot; id=&quot;submit-button&quot;&gt;&lt;/input&gt;
    &lt;/form&gt;
    &lt;!-- continue document... --&gt;
    &lt;script src=&quot;src/control.js&quot;&gt;&lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

src/script.js

const queryString = window.location.search;
const queryParams = new URLSearchParams(queryString);

const message = queryParams.get(&quot;text&quot;);

console.log(message);

// continue file...

src/control.js

const button = document.getElementById(&quot;submit-button&quot;);
button.addEventListener(&quot;click&quot;, handleText);

function handleText(event) {
  event.preventDefault();

  const text = document.getElementById(&quot;text-input&quot;).value;
  const currentURL = window.location.pathname;
  const currentDir = currentURL.substring(0, currentURL.lastIndexOf(&quot;/&quot;));

  window.location.replace(currentDir + &quot;/index.html?text=&quot; + text);
}

Hope this helps!

huangapple
  • 本文由 发表于 2023年2月6日 15:43:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定