从地址栏执行JavaScript会导致浏览器退出页面?

时间:2020-12-02 09:51:01

I'm testing a webpage, and I want to execute some JavaScript code from the address bar to change some content (e.g. changing the content of a div) I loaded the webpage and typed in the address bar the following:

我正在测试一个网页,我想从地址栏执行一些JavaScript代码来改变一些内容(例如,改变div的内容),我加载了网页,并在地址栏中输入如下内容:

javascript:document.getElementById("message").innerHTML="anotherthing"

And, after executing the above code, the browser changes the content of the div, but inmediatly it exits the page.

并且,在执行上述代码之后,浏览器会更改div的内容,但是它会自动退出页面。

How can I avoid that behavior?

我怎样才能避免这种行为呢?

Thank you.

谢谢你!

4 个解决方案

#1


5  

Add a void(0); to the end and it will fix it.

添加一个空白(0);到最后,它会修复它。

#2


6  

One more solution:

一个解决方案:

javascript: (function(){ /* Your Javascript code there */ })();

#3


0  

When you copy and paste the javascript: label into some browsers URL bars it automatically removes it. This is a security measure following certain attacks which have occurred in the past whereby people were asked to copy exploitative JavaScript into their URL bars in order for attackers to steal data.

当你复制并粘贴javascript:标签到一些浏览器的网址栏,它会自动删除它。这是在过去发生的某些攻击之后的一种安全措施,攻击者被要求将剥削性的JavaScript复制到他们的URL栏中,以便攻击者窃取数据。

If you want to execute JavaScript like this, you may find that you need to manually type out the javascript: part, otherwise your browser will assume that you're wanting to instead search for document.getElementById(...)....

如果你想执行JavaScript这样的,你可能会发现,您需要手动输入的JavaScript:一部分,否则你的浏览器会假定你想搜索. getelementbyid(…)而不是....

Example

Copy the following into your address bar:

将以下内容复制到您的地址栏:

javascript:alert('hi');

For me, in Chrome on Windows, the javascript: part is removed and instead it assumes I want to search for alert('hi'):

对于我来说,在Windows上的Chrome中,javascript: part被删除了,取而代之的是它假设我要搜索alert('hi'):

从地址栏执行JavaScript会导致浏览器退出页面?

A better solution

Depending on what you're trying to achieve, you'd probably be better off executing JavaScript through your browser's JavaScript console. If you're unsure how to do that, check this out: How to open the JavaScript console in different browsers?

根据您要实现的目标,您最好通过浏览器的JavaScript控制台执行JavaScript。如果您不确定如何做到这一点,请查看以下内容:如何在不同的浏览器中打开JavaScript控制台?

#4


0  

Alternative solution: If you don't particularly need it to be the JavaScript that changes the content, if you just need the content of the div changed, then you can use the developer tools in your browser to do that.

另一种解决方案:如果您不特别需要将内容更改为JavaScript,如果只需要更改div的内容,那么您可以在浏览器中使用开发工具来实现这一点。

In Firefox: Developer Tools > Inspector > right click the HTML and choose "Edit as HTML".

在Firefox中:开发工具>检查器>右击HTML并选择“编辑为HTML”。

In Chrome: Tools > More Tools > Developer Tools > Elements > right click the HTML and choose "Edit as HTML".

在Chrome:工具>更多工具>开发工具>元素>右键点击HTML,选择“编辑为HTML”。

In IE: Developer Tools > HTML > Edit Icon (looks like a piece of paper with a pencil)

在IE中:开发工具> HTML >编辑图标(看起来像一张纸和一支铅笔)

#1


5  

Add a void(0); to the end and it will fix it.

添加一个空白(0);到最后,它会修复它。

#2


6  

One more solution:

一个解决方案:

javascript: (function(){ /* Your Javascript code there */ })();

#3


0  

When you copy and paste the javascript: label into some browsers URL bars it automatically removes it. This is a security measure following certain attacks which have occurred in the past whereby people were asked to copy exploitative JavaScript into their URL bars in order for attackers to steal data.

当你复制并粘贴javascript:标签到一些浏览器的网址栏,它会自动删除它。这是在过去发生的某些攻击之后的一种安全措施,攻击者被要求将剥削性的JavaScript复制到他们的URL栏中,以便攻击者窃取数据。

If you want to execute JavaScript like this, you may find that you need to manually type out the javascript: part, otherwise your browser will assume that you're wanting to instead search for document.getElementById(...)....

如果你想执行JavaScript这样的,你可能会发现,您需要手动输入的JavaScript:一部分,否则你的浏览器会假定你想搜索. getelementbyid(…)而不是....

Example

Copy the following into your address bar:

将以下内容复制到您的地址栏:

javascript:alert('hi');

For me, in Chrome on Windows, the javascript: part is removed and instead it assumes I want to search for alert('hi'):

对于我来说,在Windows上的Chrome中,javascript: part被删除了,取而代之的是它假设我要搜索alert('hi'):

从地址栏执行JavaScript会导致浏览器退出页面?

A better solution

Depending on what you're trying to achieve, you'd probably be better off executing JavaScript through your browser's JavaScript console. If you're unsure how to do that, check this out: How to open the JavaScript console in different browsers?

根据您要实现的目标,您最好通过浏览器的JavaScript控制台执行JavaScript。如果您不确定如何做到这一点,请查看以下内容:如何在不同的浏览器中打开JavaScript控制台?

#4


0  

Alternative solution: If you don't particularly need it to be the JavaScript that changes the content, if you just need the content of the div changed, then you can use the developer tools in your browser to do that.

另一种解决方案:如果您不特别需要将内容更改为JavaScript,如果只需要更改div的内容,那么您可以在浏览器中使用开发工具来实现这一点。

In Firefox: Developer Tools > Inspector > right click the HTML and choose "Edit as HTML".

在Firefox中:开发工具>检查器>右击HTML并选择“编辑为HTML”。

In Chrome: Tools > More Tools > Developer Tools > Elements > right click the HTML and choose "Edit as HTML".

在Chrome:工具>更多工具>开发工具>元素>右键点击HTML,选择“编辑为HTML”。

In IE: Developer Tools > HTML > Edit Icon (looks like a piece of paper with a pencil)

在IE中:开发工具> HTML >编辑图标(看起来像一张纸和一支铅笔)