简体   繁体   English

使用 Chrome 控制台移动到新 URL 时,有没有办法让变量保持活动状态?

[英]When moving to new URL with Chrome console, is there a way to keep variables alive?

I'm totally new to JavaScript either Chrome console.无论是 Chrome 控制台,我还是 JavaScript 的新手。

My search of the literature was not enough to answer the question.我对文献的搜索不足以回答这个问题。

But I found it is hard to find any previous questions which tell me "How to prevent variable flushing(?) when you move to new URL in Chrome console".但是我发现很难找到任何以前的问题告诉我“当您移动到 ​​Chrome 控制台中的新 URL 时如何防止变量刷新(?)”。

For example, Let's say right now I'm in Google Starting Page (" https://www.google.com/ ")例如,假设我现在在 Google 起始页(“ https://www.google.com/ ”)

Then I open Chrome console, make integer variable and function like this...然后我打开 Chrome 控制台,创建整数变量并像这样运行......

a = 10

function myFunc(){
    console.log("Hello")
}

After that, I move to Yahoo homepage with this code之后,我使用此代码移动到雅虎主页

this.document.location = "https://www.yahoo.com/"

Then, as you can expect, my previously defined variable and function are GONE .然后,如您所料,我之前定义的变量和函数是GONE

When I try to call my previous variable a in Yahoo,当我尝试在雅虎中调用我以前的变量a时,

Uncaught ReferenceError: a is not defined
    at <anonymous>:1:1

Until now I'm stuck on this problem.直到现在我被困在这个问题上。

I wonder how to tell browser to keep variables and functions alive whenever moving to new URL?我想知道如何告诉浏览器在移动到新 URL 时保持变量和函数处于活动状态?

All you do in the console, you do in the context of the current page (its window global object and local scope).您在控制台中执行的所有操作都是在当前页面的上下文中执行的(其window全局对象和局部范围)。 If you change the page, this context is gone.如果您更改页面,则此上下文将消失。

But you can quickly re-initialize the state if you use a scratchpad script in the "Sources" tab (and "Snippets" subtab) of the developer tools.但是,如果您在开发人员工具的“源”选项卡(和“片段”子选项卡)中使用暂存脚本,则可以快速重新初始化状态。 Just create a new script and press Ctrl+S.只需创建一个新脚本并按 Ctrl+S。 Then you can select "Run" in the context menu of this script each time the page is changed and the state will be re-created:然后您可以在每次更改页面时在此脚本的上下文菜单中选择“运行”并重新创建状态:

在此处输入图片说明

不,你不能,但是如果你想“保存”一些值,你可以将它们发送到你的服务器(作为带有 json 的 ajax 请求)或将它们作为你的链接参数,例如https://your.server.com/save?a =valueOfA (当然,服务器必须支持这个参数并返回响应,它将为适当的变量设置适当的值)

您可以使用localStorge()将数据保存在客户端浏览器上,但它对功能不起作用,只是想提一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM