Professional Documents
Culture Documents
Create Cookie:
<html>
<head><title>Create or Set Cookies Demonstration</title></head>
<body>
<h2>Create or Set Cookies</h2>
<form action="" name="form1">
Name: <input type="text" name="t1"><br><br>
Password : <input type="password" name="t2"><br><br>
<input type="reset" value="Create Now" onclick="newCookie()">
</form>
<script>
with(document.form1){
function newCookie(){
if(t1.value != "" && t2.value != "" ){
document.cookie = "name="+t1.value;
document.cookie = "pass="+t2.value;
alert("Cookies are created.");
}
else{
1
Unit IV: Cookies and Browser Data
2
Unit IV: Cookies and Browser Data
<script>
with(document.form1){
function deleteCookie(){
document.cookie = "name="+t1.value+";expires=Thu, 01 Jan 1980 00:00:01 UTC";
document.cookie = "pass="+t2.value+"expires=Thu, 01 Jan 1980 00:00:01 UTC";
console.log("Cookies Deleted");
} } </script> </body></html>
Setting the expiration date of cookies:
<html> <head><title>Delete Cookies</title></head>
<body>
<h2>Delete Cookies</h2>
<form action="" name="form1">
Name <input type="text" name="t1"><br><br>
Address <input type="text" name="t2"><br><br>
<input type="button" value="Set Expiration Date for Cookies" onclick="setCookie()">
</form>
<script>
with(document.form1){
function setCookie(){
if(t1.value != "" && t2.value != ""){
document.cookie = "name="+t1.value+";expires=Thu, 26 September 2019 01:15:20 UTC";
document.cookie = "pass="+t2.value+"expires=Thu, 26 September 2019 01:15:20 UTC";
console.log("Cookies expiration date has been set");
}
else
{
alert("Please enter some values");
} } }
</script>
</body>
</html>
4.2 Browser: Opening a Window, giving a focus to new window, window position, closing
a window:
The open() method opens a new browser window, or a new tab, depending on your browser
settings and the parameter values.
Example
<html>
<head><title>Window open() method</title></head>
<body>
<h2>Window open() method</h2>
<input type="button" value="Open Window" onclick="windowOpen()">
<script>
3
Unit IV: Cookies and Browser Data
var win;
function windowOpen() {
win = window.open("","_blank", "resizable=no, left=200, top=400,width=400, height=450");
}
</script>
</body>
</html>
The focus() method sets focus to the current window.
The blur() method removes focus from the current window.
The close() method closes the current window.
Example:
<html><head><title>Window open(), close(), blur() and focus() method</title></head>
<body>
<h2>Windows open, blur, focus, close and set position </h2>
<input type="button" value="Open Window" onclick="windowOpen()">
<input type="button" value="Blur Window" onclick="windowBlur()">
<input type="button" value="Focus Window" onclick="windowFocus()">
<input type="button" value="Close Window" onclick="windowClose()">
<script>
var win;
function windowOpen() {
win = window.open("","_blank", "width=400, height=450");
}
function windowBlur() {
win.blur();
}
function windowFocus() {
win.focus();
}
function windowClose(){
win.close();
}
</script></body></html>
4
Unit IV: Cookies and Browser Data
}
function orignalContent(){
document.getElementById("show").innerHTML = "This is HTML content."
} </script> </body> </html>
Scrolling a Webpage:
<html>
<head><title>Scrolling a webpage.</title>
<style>
body{ width: 2000px; height: 2000px; }
</style>
</head>
<body>
<h2>Click to button for Scrolling this window</h2>
<input type="submit" value="Scroll window" onclick="scrollNow()">
</body>
<script>
function scrollNow(){
scrollTo(1500,1500);
}
</script>
</html>
5
Unit IV: Cookies and Browser Data
Javascript in URLs:
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.
A JavaScript URL will contain JavaScript statements that perform actions but return no
value.
For example: javascript:alert("Hello World!")
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
tag, then the JavaScript code in the URL will be executed when the user submits the form.
For security reasons the recent browsers are not allowing to execute the javascript code in
URLs.
6
Unit IV: Cookies and Browser Data
JavaScript Security:
One of the most common JavaScript security vulnerabilities are Cross-Site Scripting (XSS).
Cross-Site Scripting vulnerabilities enable attackers to manipulate websites to return malicious
scripts to visitors. These malicious scripts then execute on the client side in a manner
determined by the attacker. XSS vulnerabilities can exist when browser or application authors
fail to implement the same origin policy and can be prevented by following correct development
techniques. If XSS vulnerabilities aren’t remediated, they can result in user data theft, account
tampering, malware spreading or remote control over a user’s browser.
There are a variety of other common JavaScript security issues that can increase risks for
users. These issues include improper client-server trust relationships, vulnerabilities in browser
and browser plugin code, and incorrect implementation of sandboxing or same origin policy.
And for Node.js-based server side applications, there may be many other security
vulnerabilities, including SQL Injection, Command Injection, and others. The only way for
organizations to avoid these JavaScript security risks is to develop and source applications that
are free of JavaScript security vulnerabilities. Many organizations use JavaScript security
analyzers to test for and remediate these vulnerabilities.
7
Unit IV: Cookies and Browser Data
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Execute a Function after Some Time</title>
</head>
<body>
<script>
function myFunction() {
alert('Hello World!');
}
</script>
<button onclick="setTimeout(myFunction, 2000)">Click Me</button>
</body>
</html>
The above example will display an alert message after 2 seconds on click of a button.
8
Unit IV: Cookies and Browser Data
9
Unit IV: Cookies and Browser Data
Window History
The window.history object can be written without the window prefix.
To protect the privacy of the users, there are limitations to how JavaScript can access this
object.
Some methods:
history.back() - same as clicking back in the browser
history.forward() - same as clicking forward in the browser
Window History Back
The history.back() method loads the previous URL in the history list.
This is the same as clicking the Back button in the browser.
Example
Create a back button on a page:
<html>
<head>
<script>
function goBack() {
window.history.back()
}
</script>
</head>
<body>
<input type="button" value="Back" onclick="goBack()">
</body>
</html>
10