javascript - 如何使用 JavaScript 重定向将值传递给 HTML 页面?

标签 javascript html

我有以下功能:

function loginUser(form){
  try {
    var user = Backendless.UserService.login(form.userid.value, form.pswrd.value);
    if (user != null) {
      window.location.href = "home.html";
    } else
      window.alert("Login failed");
    }
  } catch (e) {
    window.alert("Login failed. " + e.message);
  }
}

当我重定向到 home.html 时,用户对象有各种字段,我想将 user.credits 的值传递给 HTML 页面上的输入框.我该怎么做呢?

这是我的 HTML 页面的样子:

<div>Credits <input id="credits" type="text" class="input-block-level" placeholder="Credits" name="credits"></div>

最佳答案

你可以

window.location.href = "home.html?credits="+user.credits;

在你的 home.html 上:

document.getElementById('credits').value=getUrlVars()["credits"];

您可能想要了解一下 GET 和 POST 变量的概念。 另请注意,此方法绝不安全:用户可以轻松操纵该值。

更安全的方法是在目标页面上POST 值,或者更好的方法是通过 PHP 等方式读取值。

关于javascript - 如何使用 JavaScript 重定向将值传递给 HTML 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31704011/

相关文章:

javascript - 动态添加 span 标签到 <ng-content></ng-content> 内的链接

javascript - 为什么这段代码不能用于字谜检查?

JavaScript 逻辑没有贯彻到底

javascript - 菜单选项卡按钮不起作用

html - 如何重新创建 "image on the left, text on the right"和 "text on the right image on the left"模式?

javascript - 带有 if-else 函数的 Onclick 事件在 IE8 中不起作用

javascript - 带有 InfoBox 插件的 Google Maps API v3 事件鼠标悬停

javascript - 网站显示为不安全,因为流媒体提供商不支持 SSL

javascript - 如何使用jquery选择表中没有ID和类的嵌套元素

php - <head profile ="http://gmpg.org/xfn/11"> 这是什么?