Javascript: the pseudo-Protocol qualifier indicates that the URL content is an arbitrary string of the JavaScript code to be run by the javascript interpreter. A javascript URL is as follows:
javascript:var now=new Date();"
When a browser loads such a javascript URL, it will execute the JavaScript code contained in the URL and convert it into a string using the value of the last JavaScript statement or expression, as the content of the newly loaded document. This string value may contain HTML tags and be formatted and displayed as other documents loaded into the browser.
Javascript URLs can also contain JavaScript statements that execute operations but do not return values. For example:
javascript:alert("Hello World !");
When this type of URL is loaded, the Browser executes Javascript code, but because there is no value to display as a new document, it does not change the currently displayed document.
Generally, programmers may want to use a javascript URL to execute some JavaScript code without changing the currently displayed document. To achieve this, make sure that the last statement in the URL does not return a value. One way to ensure this is to explicitly specify an undefined return value using the void operator. You only need to use void 0 at the end of the javascript URL ;. For example, the following URL opens a new blank browser without changing the content of the current window:
javascript:window.open("about:blank");void 0;
If the URL does not contain the void operator, window. the return value of an open () method call will be converted to a string and displayed. The current document will be overwritten by the new document. The new document will display the following content:
[object Window]
Javascript: the pseudo protocol can be used with the HTML attribute. The value of this attribute should also be a URL. The href attribute of a hyperlink meets this condition. When a user clicks such a link, the specified JavaScript code is executed. In this case, the javascript URL is essentially an onclick event handle replacement.
<a href="javascript:alert('Hello World !')">test</a>
Similarly, a javascript URL can be used as the action attribute of the <form> tag. When a user submits this form, the JavaScript code in the URL is executed:
<body><form action="javascript:alert('Hello World !')"><input type="submit" value="confirm"></form></body>