简体   繁体   中英

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.

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".

For example, Let's say right now I'm in Google Starting Page (" https://www.google.com/ ")

Then I open Chrome console, make integer variable and function like this...

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 .

When I try to call my previous variable a in Yahoo,

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?

All you do in the console, you do in the context of the current page (its window global object and local scope). 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. 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()将数据保存在客户端浏览器上,但它对功能不起作用,只是想提一下

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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