从地址栏执行 JavaScript 会导致浏览器退出页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30077717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing JavaScript from the address bar causes the browser to exit the page?
提问by fergaral
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.
谢谢你。
采纳答案by potatopeelings
Add a void(0); to the end and it will fix it.
添加一个 void(0); 到最后,它会修复它。
回答by Venkatesh Achanta
One more solution:
另一种解决方案:
javascript: (function(){ /* Your Javascript code goes here */ })();
回答by James Donnelly
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:
标签复制并粘贴到某些浏览器的 URL 栏中时,它会自动将其删除。这是在过去发生的某些攻击之后采取的安全措施,人们被要求将剥削性的 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:
部分,否则你的浏览器会认为你想要搜索document.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:
部分被删除,而是假设我想搜索alert('hi')
:
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 控制台?
回答by BSMP
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 > 编辑图标(看起来像一张带铅笔的纸)