please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 10
Client-Side Program Structure
Next
 

10.4 JavaScript in URLs

Another way that JavaScript code can be included on the client side is in a URL following the javascript: pseudo-protocol specifier. This special protocol type specifies that the body of the URL is arbitrary JavaScript code to be interpreted by the JavaScript interpreter. If the JavaScript code in a javascript: URL contains multiple statements, the statements must be separated from one another by semicolons. Such a URL might look like the following:

javascript:var now =index.html new Date(); "<h1>The time is:</h1>" + now;

When the browser "loads" one of these JavaScript URLs, it executes the JavaScript code contained in the URL and displays the "document" referred to by the URL. This "document" is the string value of the last JavaScript statement in the URL. This string will be formatted and displayed just like any other document loaded into the browser.

More commonly, a JavaScript URL will contain JavaScript statements that perform actions but return no value. For example:

javascript:alert("Hello World!")
When this sort of URL is "loaded," the browser executes the JavaScript code, but, because there is no value to display as the new document, it does not modify the currently displayed document.

Note that in Navigator 3.0, you can use the void operator to force an expression to have no value. This is useful when you want to execute an assignment statement, for example, but do not want to display the assigned value in the browser window. (Recall that assignment statements are also expressions, and that they evaluate to the value of the right-hand-side of the assignment.)

The javascript: URL can be used anywhere you'd use a regular URL. It is not altogether clear, however, why you'd want to do so. In Navigator, one important use for this syntax is typing it directly into the Location field of your browser, where it allows you to try out and test arbitrary JavaScript code without having to get out your editor and create an HTML file containing the code. In fact, Navigator takes this idea even further. As described in Chapter 1, Introduction to JavaScript, if you enter the URL javascript: alone, with no JavaScript code following it, Navigator displays a JavaScript interpreter page that allows you to sequentially enter and execute lines of code. Unfortunately, neither of these techniques work in Internet Explorer 3.0.

javascript: URLs can also be used in other contexts. You might use one as the target of a hypertext link, for example. Then when the user clicks on the link, the specified JavaScript code will be executed. Or, if you specify a javascript: URL as the value of the ACTION attribute of a <FORM> tag, then the JavaScript code in the URL will be executed when the user submits the form. In these contexts, the javascript: URL is essentially a substitute for an event-handler. Event handlers and javascript: URLs can often be used essentially interchangeably, and which you choose is basically a stylistic matter.

There are a few circumstances where a javascript: URL can be used with objects that do not support event handlers. For example the <AREA> tag does not support an onClick() event-handler on Windows platforms in Navigator 3.0 (one will be added in the next release, though). So if you want to execute JavaScript code when the user clicks on a client-side image map, you must use a javascript: URL.

Internet Explorer supports the javascript: protocol specifiers for URLs, but does not have a special built-in JavaScript interpreter page. A future version of Explorer will probably also support a vbscript: protocol.


Previous Home Next
JavaScript and Events Book Index JavaScript Entities

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/JAVA/SCRIPT/definitive/ch10_04.htm, 7KB, , updated: 2019/10/14 15:00, local time: 2024/12/19 05:15,
TOP NEW HELP FIND: 
3.128.79.4:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://massmind.org/techref/language/JAVA/SCRIPT/definitive/ch10_04.htm"> [Chapter 10] 10.4 JavaScript in URLs</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .