Javascript in URL

Source: Internet
Author: User

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>
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.