Pseudo-Protocol JavaScript in javascript: using to explore _javascript techniques

Source: Internet
Author: User

The way to add JavaScript code to the client is to place it in the pseudo protocol descriptor javascript: the URL that follows. This particular protocol type declares that the principal of the URL is any JavaScript code, which is run by the JavaScript interpreter. If the JavaScript code in Javascript:url contains more than one statement, you must separate the statements with semicolons. Such URLs are as follows:

Javascript:var now = new Date (); " 

When the browser loads such a URL, it executes the JavaScript code contained in the URL and displays the string value of the last JavaScript statement as the contents of the new document. This string value can contain HTML tags and is formatted to display exactly the same as other documents loaded into the browser.

JavaScript URLs can also contain JavaScript statements that perform only actions but do not return values. For example:

Javascript:alert ("Hello world!")

When this URL is loaded, the browser executes only the JavaScript code in it, but it does not change the currently displayed document because it does not have a value to display as a new document.

Usually we want to use Javascript:url to execute some JavaScript code that does not change the currently displayed document. To do this, you must ensure that the last statement in the URL does not return a value. One method is to explicitly specify the return value as underfined with the void operator, using statement void 0 only at the end of the Javascript:url. For example, the following URL opens a new, empty browser window without changing the contents of the current window:

Javascript:window.open ("About:blank"); void 0;

If the URL does not have a void operator, the return value of the window.open () method is converted to a string and displayed, and the current window will be overwritten by the document shown below.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.