Put your text ad here
WestNIC provides reliable web hosting services
Web hosting directory, find affordable web hosting
Fastwebhost offers cheap web hosting & reseller hosting services
This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQ answers.
script type and language attributes?
.jds?
for each loop run in IE?
onclick event on my button is not working!
script type and language attributes?
A: The script element's type attribute specifies the content type of the script, which should always be text/javascript, the standard Multipurpose Internet Mail Extensions (MIME) type for Javascript. The "text" part signifies that the script is essentially a plain text format. The "javascript" part specifies the scripting language.
The script element's language attribute is now deprecated, which means that it should no longer be used in HTML. The language attribute used to specify a particular version of the Javascript language required to run the script, to provide a fall-back mechanism for Web browsers that do not support more advanced scripting features. This was never a particularly good way to ensure script compatibility. Instead, you should use object, method and property detection to test the availability of specific scripting features before use. See Golden rules for Javascript for a guide to these defensive coding techniques.
A: The language we use for client side scripting on the Web is commonly known as Javascript and this is the identifier you should use in HTML script elements. There is a number of other client side scripting languages that have been developed for the Web, but Javascript is the only one that is supported in the majority of Web browsers. The standard syntax, language features and logic rules of Javascript are defined in the ECMA-262 specification, which is the origin of the less common name “ECMA script”.
In general terms there is not a significant difference between scripting languages and general purpose programming languages. Javascript is designed to be executed in the context of another application, a Web browser, and it is not compiled before it is executed, but these are minor distinctions. Some Web browsers do compile Javascript into their own executable format, but this is not a necessary or inherent part of the language itself.
.jds?
A: On the Web, the file extension you use for Javascript is not as important as the Content-Type header it is served with. With Apache, add this line to your .htaccess configuration file:
AddType "application/x-javascript; charset=iso-8859-1" jds
It is certainly best to separate your Javascript from your HTML like this in most cases. Firstly, it makes the script easier to edit and maintain, and means that any number of HTML pages can reference the same script. Normally, a shared external script will only be downloaded once for each visitor, which speeds up delivery of your page.
A: It is conventional to put HTML comments around Javascript element content to hide it from browsers, like so ...
… full answer hidden, click for full answers
Premium members click below for full answer
My Javascript code is showing!
A: One of the key steps in externalising scripts is to ensure that all script file references are accurate.
The Code Style site uses relative URL paths with a leading forward slash, /; this means the URL is relative to the root level of the domain, not the current directory or “folder”. If you are adapting Code Style examples on a local file system, you will need to change these file references accordingly.
The following examples use stylesheet references, but the same scheme applies to all external files including scripts and images.
./Style.css
Style.css in same directory as the HTML source, the "current" directory.
styles/Style.css
Style.css in a subdirectory of the current directory called styles.
../styles/Style.css
Style.css in a parallel directory called styles at the same hierarchical level as the current directory.
To check if an external script file has loaded, add the following code to the top of the file and reload the HTML in the browser:
// Debugging alert('Script loaded.');
This should raise a dialogue box if the script loads (you may need to clear your browser's cache to force a reload). Once you're sure the script has loaded, put other temporary alerts into the script to find out which parts execute and which fail.
A: It would help to see a fragment of the code. One thing to watch out for is the use of a single assignment equals instead of the double comparison equals...
… full answer hidden, click for full answers
Premium members click below for full answer
I can't get Javascript to check for null strings correctly!
A: The message "Done, but with errors on page" is an Internet Explorer error message that means there was a problem with one or more of the Javascript statements on the page. Normally you will see a warning icon alongside the error message in the status bar. If you double click on the icon, a status message will open to give details of the problem, which may help you fix the errors.
for each loop run in IE?
A: There are a few critical points in your code that might fail if conditions are not as you expect. One simple but effective approach is to insert Javascript alert() statements at points through the loop to check the outcome of each condition, e.g.
if(data) { alert('The data variable evaluates to true.');// Else none of the following executes......
It is not clear where the variable data originates and whether you would always expect it to have a value. Where does the key reference below originate, what does it refer to?
...for(keyindata[i]) { alert('Data value '+ i +' = '+ data[i]); } ...
Does this loop run at all? What values do you get? This simple alert() based technique can give quick insights to where your code may fail. Some browser add-ons and developer consoles allow you to step through Javascript as it executes and inspect values in more detail.
onclick event on my button is not working!
A: In a situation like this the failure of your Javascript could originate at any number of levels. It is very important to see the whole system to understand what is going on, but these basic debugging steps should help.
… full answer hidden, click for full answers
Premium members click below for full answer
The onclick event on my button is not working!
A: Web browsers' Window object references only extend as far as windows in the same frameset or "child" windows spawned from a specific document. It would be a user security violation if a script could read any other window's URL because this information could be communicated back to the script's host. For instance, you could append the URL to the query parameter of a second page on the script's host, redirect to that URL and log or process the request.
A: It is not possible to hide the address bar from an existing browser window, only for a new "pop-up" window created by Javascript. Having said that, many people use pop-up blockers, so you should think carefully before you use new windows. Tabbed browsers may be configured to create a new tab in this case and some might show the address regardless. For example, Firefox and Opera show the current address at the top of the window, but it is not editable.
In any case, the address of the page will be accessible from the browser history list and by inspecting the source code of the page. For these reasons it is important to test across a number of target browsers. It might be better to use a lightbox-style overlay for dynamically generated content or possibly an iframe for page content.
The features of the new browser window are set by the third argument of the window.open() method, after the window's URL and target name reference, as below. Several other features can be configured but should be used with caution because they can severely restrict the usability of the new browser window.
… full answer hidden, click for full answers
Premium members click below for full answer
Can we hide the browser address bar?
A: There are two elements to this question, first to find the difference between two dates and second to format the result. The code below creates three variables to represent time intervals in milliseconds, then creates two dates with specific times on the same day. This could be adapted for any two dates, including the current time using the basic Date() constructor.
… full answer hidden, click for full answers
Premium members click below for full answer
How do I get the difference between two dates in Javascript?
A: The example below creates a new Date object and a set of constants to calculate the number of milliseconds in 7 days. The 7 day figure is added to the original time and used to create a second Date object.
… full answer hidden, click for full answers
Premium members click below for full answer
How can I add 7 days to a date in Javascript?
A: It is not possible to access files on a person's computer using Javascript through a Web browser, or to read the properties of files. It would be a serious security and privacy hazard if Javascript code in a Web page could read files on your computer because the information could be sent back to the site via a form or other mechanisms. For these reasons the possibility of file access was excluded when the Javascript language was designed.
Other client side solutions such as ActiveX and Java applets require special security consent from the user, which many people are reluctant to grant, and only make the job more complicated and risky from their point of view. Not all Web browsers are configured to support ActiveX or Java.
The only effective way to check the size of a file for upload is on the server side. Your server side form handler must process the file and this is the best place to apply a limit to the number of bytes you will accept.
A: It is not possible to create files on the client's computer using Javascript. This type of action is excluded from the Javascript language because it would be a serious security hazard. The only form of local file storage available through Javascript is cookies, whose behaviour and contents are strictly prescribed.
Of course it is possible to use Javascript to generate file content in a Web browser window and invite people to save the contents. Internet Explorer and Firefox users can use the File menu Save as... option directly, but Opera and other browser users may have to copy the text into another application to save it.
… full answer hidden, click for full answers
Premium members click below for full answer
How can I create a file on the client side?
| Front-end FAQs | Back-end FAQs | Learn Java |
|---|---|---|
About us: site help, text ads and premium content FAQs.