Episode#3 Topics:-
- Why NoScript tag important? How to use this tag?
- What is a navigator in javascript? How to use this?
- Example to know browser properties.
- Example to know flash player installed or not in your system.
- Example to know your location in javascript.
If u r making a website with the help of javascript, u must use <noscript></noscript>
Why it's important to use the NoScript tag by developers?
- Website users will not be allowed to use features made by JS code.
- It may decrease no of users on your website
- It may prevent you from attack. like you made a form where all validations added/written in javascript. so in this case your validation will fail.
Navigator in JavaScript is an object that provides details about the browser that the user uses to access the web application. As we have Document Object Model (DOM) to have control over manipulating the data, similarly, we have Browser Object Model (BOM) that provides us to with the control on how applications are viewed on the browser.
The navigator object is a window property that can be accessed by
window.navigator or navigator
Table 1: Properties of Navigator Object
Below are mentioned some navigator object properties
Property | Description |
appCodeName | Returns the code name of the browser |
appName | Returns the name of the browser |
appVersion | Returns the version information of the browser |
cookieEnabled | Determines whether cookies are enabled in the browser |
geolocation | Returns a Geolocation object that can be used to locate the user’s position |
language | Returns the language of the browser |
online | Determines whether the browser is online |
platform | Returns for which platform the browser is compiled |
product | Returns the engine name of the browser |
userAgent | Returns the user-agent header sent by the browser to the server |
Table 2: Methods of Navigator Object
Method | Description |
javaEnabled() | Specifies whether or not the browser has Java enabled |
taintEnabled() | Removed in JavaScript version 1.2. Specifies whether the browser has data tainting enabled |
Data tainting allows one window to see the properties in another window and is removed since it proved to be a high-security risk.
Example #1
Navigator Properties and Methods
Code:
<!doctype html>
<script>
document.write("<strong>Code Name of the Browser</strong> : ",navigator.appCodeName + "<br><br>");
document.write("<strong>Name of the Browser</strong> : ",navigator.appName + "<br><br>");
document.write("<strong>Cookies Enabled</strong> : ",navigator.cookieEnabled + "<br><br>");
document.write("<strong>Platform of the Browser</strong> : ",navigator.platform + "<br><br>");
document.write("<strong>Browser in onLine Mode</strong> : ",navigator.onLine + "<br><br>");
document.write("<strong>Java Enabled</strong> : ",navigator.javaEnabled());
</script>
</html>OUTPUT:-
Code Name of the Browser : Mozilla
Name of the Browser : Netscape
Cookies Enabled : true
Platform of the Browser : MacIntel
Browser in onLine Mode : true
Java Enabled : false
Table 3: Collections of Navigator Object
The table below lists the collections present in the JavaScript navigator object and then we will see one example of it.
Collection | Description |
plugins[] | returns a reference to all the embedded objects in the document |
mimeTypes | returns a collection of MIME types supported by the client browser |
The mime property has three predefined fields:
- name – the name of the MIME type (video/mpeg)
- description – description of the type
- suffixes – list of all possible file suffixes(extensions of file) for the MIME type.
Example #2
JavaScript Navigator Collection
Code:
<!doctype html>
<script>
var plugin = navigator.plugins["Flash Player"];
if (plugin)
document.write("Plugin Present")
else
document.write("You don't have Flash Player installed!")
</script>
</html>
Example# 3
JavaScript Navigator Property – geolocation
Code:
<!DOCTYPE html>
<html>
<body>
<script>
navigator.geolocation.getCurrentPosition(showPosition);
function showPosition(position) {
console.log("Latitude: " + position.coords.latitude + "\n" +"Longitude: " + position.coords.longitude);
}
</script>
</body>
</html>
Comments
Post a Comment