JavaScript pseudo-protocol in javascript: How to Use it _ javascript tips-js tutorial

Source: Internet
Author: User
Javascript: This special protocol type declares that the URL subject is any javascript code, the method for adding javascript code to the client by running the javascript interpreter is to place it in the URL after the pseudo-Protocol interpreter javascript. This special protocol type declares that the subject of the URL is any javascript code, which is run by the javascript interpreter. If javascript: the javascript code in the URL contains multiple statements, you must use semicolons to separate these statements. The URL is as follows:

javascript:var now = new Date(); "The time is:" + now;

When the browser loads such a URL, it will execute the javascript code contained in the URL and display the string value of the last javascript statement as the content of the new document. This string value can contain HTML tags and be formatted. It is displayed exactly the same as other documents loaded into the browser.

Javascript URLs can also contain javascript statements that only execute actions but do not return values. For example:

javascript:alert("hello world!")

When such a URL is loaded, the browser only executes the javascript code in it, but it does not change the currently displayed document because it is not displayed as a value for the new document.

We usually want to use javascript: URL to execute some javascript code that does not change the currently displayed document. To do this, make sure that the last statement in the URL does not return a value. One way is to explicitly specify the return value as underfined using the void operator. You only need to use the void 0 statement at the end of javascript: URL. For example, the following URL opens a new blank browser window without changing the content of the current window:

javascript:window.open("about:blank"); void 0;

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

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.