如何通过在地址栏中键入一些 HTML 来显示它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6918345/
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
How to display some HTML by typing it into the address bar?
提问by ignis
Is there a way to have the browser display a given HTML page by pasting the HTML code into the address bar and then typing Enter? Browser is Google Chrome. Unfortunately, if I paste the HTML and then hit enter, it redirects to the Google search site... which is not what I want!
有没有办法通过将 HTML 代码粘贴到地址栏中然后键入 Enter 来让浏览器显示给定的 HTML 页面?浏览器是谷歌浏览器。不幸的是,如果我粘贴 HTML 然后按 Enter,它会重定向到 Google 搜索站点...这不是我想要的!
I want to paste the HTML code, such as <html><body>...</body></html>
我想粘贴 HTML 代码,例如 <html><body>...</body></html>
Another option I thought about is to paste something like
我想到的另一个选择是粘贴类似的东西
javascript:document.write('<html><body>...</body></html>');
javascript:document.write('<html><body>...</body></html>');
but that appendsthe HTML to the current page, while I want to resetthe page contents instead.
但这会将HTML附加到当前页面,而我想重置页面内容。
回答by Ray
You can use data:text/html,
an example:
你可以使用data:text/html,
一个例子:
data:text/html,<h1>Hello World</h1><script>alert('JavaScript works too!');</script>
Other examples of data URIs: https://developer.mozilla.org/en/data_URIs
数据 URI 的其他示例:https: //developer.mozilla.org/en/data_URIs
回答by avetarman
javascript:document.write("your code here");
回答by SeriousSamP
Or, to make things even easier if you need different HTML displayed at different times, try putting this in the address bar then entering the HTML into the prompt. This JavaScript that goes in the address bar is called a bookmarklet, and if you need to do this often you can add it as a bookmark/favourite for easy access.
或者,如果您需要在不同时间显示不同的 HTML,为了使事情变得更容易,请尝试将其放在地址栏中,然后在提示中输入 HTML。地址栏中的这个 JavaScript 称为书签,如果您需要经常这样做,您可以将其添加为书签/收藏夹以便于访问。
javascript:document.write(prompt('Enter the HTML', ''));
回答by Demian Brecht
URL: javascript:document.write('foo');
网址: javascript:document.write('foo');
You can display HTML data using DOM manipulation, but you can't directlyinput HTML.
您可以使用 DOM 操作来显示 HTML 数据,但不能直接输入 HTML。
回答by Mark Cidade
javascript:void(document.write("<html style='color:red'>HTML goes here</html>"))