Location, Navigator and Cookie variables tutorial in JavaScript
6 minute(s) read | Published on: Jun 04, 2021 Updated on: Dec 14, 2021 |
JavaScript programming language is so popular among programmers and the reason for its popularity is that there are many features in this language and if they are being used, they would help you attract many users to the programs or sites, for example, through the window.location object, it is possible to get the current site address and also redirect the browser to a new page, which can be very useful, in addition to this, there are other things that here some of them are mentioned, but before that we need to give a brief explanation about JS.
What is JS?
It stands for JavaScript which is a language that is being used in order to convert a page from static to dynamic, which finally attract more users to the site, as you know, the main goal of owners of various programs and sites is to be able to attract more visitors and eventually succeed in this way, so that they can get help from this language, in order to achieve this goal.
Window Location:
It is one of the features available in this language that can be used to receive the current website address and redirect the browser to a new page, it should be noted that it is possible to write it without window prefix, now pay attention to the following points.
- window.location.href:
Used to return the current page href.
- window.location.pathname:
As its name implies, it is used to return the path and name of the current page.
- window.location.hostname:
This can be used to return the web host domain name.
- window.location.protocol:
It is used to return
the web protocol
.
- window.location.port:
It returns the number of the internet host port.
- window.location.assign:
This item is used to load a new document.
In order to understand the points which have been mentioned above, it is necessary to give an example for one of the cases, consider the following example, which is related to window.location.protocol.
The result of the example will be as follows.
JavaScript
The window.location object
The page protocol is: https:
JavaScript Window Navigator:
It is used in order to get information about the user's browser.
Note the following:
- cookieEnabled:
This is used to check for cookies, and after the check is done, it returns true if cookies are enabled, and false if cookies are not enabled, now consider the following example.
The result of the example is as follows, showing that the cookies were enabled.
The Navigator Object
The cookieEnabled property returns true if cookies are enabled:
navigator.cookieEnabled is true
- appName:
It is used to return
the application
name of the browser, so that you can understand the following example better.
The result of the example will be as follows.
Object
The appName property returns the application name of the browser:
navigator.appName is Netscape
Strange enough, "Netscape" is the application name for IE11, Chrome, Firefox, and Safari.
- appCodeName:
This item is also used to return the code name of the browser, which you will see more by looking at the example below.
The result of the example will be as follows.
JavaScript-Navigator
The appCodeName property returns the code name of the browser.
Do not rely on it! "Mozilla" is the application code name for Chrome, Firefox, IE, Safari, and Opera.
navigator.appCodeName is Mozilla
- product:
This is also used when you want to return the product name of the browser engine.
The result of the above example will be as follows.
The Navigator-Object
The product property returns the product name of the browser.
Do not rely on it! Most browsers returns "Gecko" as product name!
navigator.product is Gecko
- appVersion:
This item is used for version information about the browser.
The result of the above example will be as follows.
The Navigator-Object
The appVersion property returns version information about the browser:
5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
- userAgent:
This item is used to return the user-agent header sent by the browser to the server, which you can understand it better if you pay attention to the following example.
The result of the example will be as follows.
The Navigator-Object
The userAgent property returns the user-agent header sent by the browser to the server:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
- platform:
This item can be used to return the operating system.
The result of the example will be as follows.
this Object
The platform property returns the browser platform (operating system):
navigator.platform is Win32
- language:
As its name implies, this is used to return the browser's language.
The result of the above example will be as follows.
this Object
The language property returns the browser's language:
navigator.language is en-UK
- online:
In this regard, if the browser is online, it returns true, you can understand this by considering the following example.
The result of the example will be as follows.
this Object
The onLine property returns true if the browser is online:
navigator.onLine is true
- javaEnabled ():
In this case, if Java is enabled, it returns true.
The result of the example if Java is disabled would be as follows.
The Navigator-Object
The javaEnabled() method returns true if Java is enabled:
javaEnabled is false
JavaScript-Cookies:
Cookies can actually remember information about
a user who has visited the page
and are stored as small text files on the computer, by using JavaScript, it is possible to create different cookies, it also allows you to read, modify, delete, etc. data.
- document.cookie:
When you try to write a whole string in a cookie, and when you want to read it again, you can only see its name and value pair, and setting a new cookie will not overwrite old ones.
Last word:
You know that there are many possibilities in JavaScript, each of which has a huge world, and if you master this world, it will be possible for you to achieve success in
the field of programming
, this is why we tried to mention the important points about Window Location, Window Navigator and JavaScript Cookies, so that you can do better programming with the help of these items.
Click to analyze your wesbite SEO