JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

13.2.5 JavaScript in URLs

13.2.5 JavaScript in URLs


Another way that JavaScript code can be included on the client side is in a URL following the javascript: protocol specifier. This special protocol type specifies that the body of the URL is an arbitrary string of JavaScript code to be run by the JavaScript interpreter. It is treated as a single line of code, which means that statements must be separated by semicolons and that /* */ comments must be used in place of // comments. The “resource” identified by a javascript: URL is the return value of the executed code, converted to a string. If the code has an undefinedreturn value, the resource has no content.

You can use a javascript: URL anywhere you’d use a regular URL: the href attribute of an tag, the action attribute of a, for example, or even as an argument to a method like window.open(). A JavaScript URL in a hyperlink might look like this:

What time is it?

Some browsers (such as Firefox) execute the code in the URL and use the returned string as the content of a new document to display. Just as when following a link to an http: URL, the browser erases the current document and displays the new one. The value returned by the code above does not contain any HTML tags, but if it did, the

13.2 Embedding JavaScript in HTML | 315

browser would have rendered them as it would have rendered the equivalent conventionally loaded HTML document. Other browsers (such as Chrome and Safari) do not allow URLs like the one above to overwrite the containing document—they just ignore the return value of the code. They do, however, still support URLs like this one:

Check the time without overwriting the document

When this sort of URL is loaded, the browser executes the JavaScript code, but because there is no returned value (the alert()method returns undefined), browsers like Firefox do not replace the currently displayed document. (In this case, the javascript: URL serves the same purpose as an onclick event handler. The link above would be better expressed as an onclick handler on aelement—the element should generally be reserved for hyperlinks that load new documents.) If you want to ensure that a javascript: URL does not overwrite the document, you can use the void operator to force an invocation or assignment expression to be undefined:

Open Window

Without the void operator in this URL, the return value of the Window.open() method call would (in some browsers) be converted to a string and displayed, and the current document would be overwritten by a document that contains this text:

[object Window]

Like HTML event handler attributes, JavaScript URLs are a holdover from the early days of the Web and are generally avoided in modern HTML. javascript: URLs do have a useful role to play outside of HTML documents. If you need to test a small snippet of JavaScript code, you can type a javascript: URL directly into the location bar of your browser. Another legitimate (and powerful) use of javascript:URLs is in browser bookmarks, as described below.

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/31413.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1