Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display some HTML by typing it into the address bar?

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!


I want to paste the HTML code, such as <html><body>...</body></html>

Another option I thought about is to paste something like

javascript:document.write('<html><body>...</body></html>');

but that appends the HTML to the current page, while I want to reset the page contents instead.

like image 229
ignis Avatar asked Aug 02 '11 20:08

ignis


People also ask

How do I display HTML code in browser?

To view only the source code, press Ctrl + U on your computer's keyboard. Right-click a blank part of the web page and select View source from the pop-up menu that appears.

How do I view HTML in Chrome?

Fire up Chrome and jump to the webpage you want to view the HTML source code. Right-click the page and click on “View Page Source,” or press Ctrl + U, to see the page's source in a new tab. A new tab opens along with all the HTML for the webpage, completely expanded and unformatted.


4 Answers

You can use data:text/html, an example:

data:text/html,<h1>Hello World</h1><script>alert('JavaScript works too!');</script>

Other examples of data URLs: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs

like image 117
Ray Avatar answered Oct 13 '22 15:10

Ray


javascript:document.write("your code here");
like image 25
avetarman Avatar answered Oct 13 '22 16:10

avetarman


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.

javascript:document.write(prompt('Enter the HTML', ''));
like image 41
SeriousSamP Avatar answered Oct 13 '22 15:10

SeriousSamP


URL: javascript:document.write('foo');

You can display HTML data using DOM manipulation, but you can't directly input HTML.

like image 45
Demian Brecht Avatar answered Oct 13 '22 15:10

Demian Brecht