"button" value = "open new window" onclick = "openWindow()"/> |
wnt 08, sgoripting Language (MSBTE) ag
Z Cookies and Browser Data
ut!
ae
iS “~» =
=|
| Click to open a new window
In Program 42.3, the statement ‘window.open(”","","left=0, top=0, width=200,height=100")is used to open a window
and display it to the specified position. The left and top properties specify the x and y coordinates respectively.
424 Changing the Content of Window
{How fo change the c
1 Howto change the content of whole window? Explain with example.
(2. How to change the content of a particular element of @ webpage? Explain with exemple.
Youcan change the content of window dynamically. You can change content in two ways; first changing the content of
vihol window, second changing the content of a specific element present in the window,
42.4.1 Changing the Content of whole Window
You can change the content of whole window by using document.write() function. This function is used to override
the content of the webpage.
Program 4.2.4: Write a JavaScript code to change the content of whole window.
Solution :
jshtmi>
\sheads
ser aa
‘Script type = "text/javascript">
function changeContent() {
document tite-
-
After licking the Button: New Content
"); ;
write ;
}
Sseript>
TettooaieagtCooklos and Browser
a
N¥_ciiont Sito Scripting Language (MSBTE)
f=fiead>
j
J
Click to cha
Atter clicking the Button: New Content
");’statement Is useq
0
ge the content of whole. Window
*Change Content” onclic
icking the Button: Old Content
k= "changeContent()"/>
In Program 4.2.4
change whole content of the webpage.
Output:
Co L @ filey/e:/
Click to change the content of whole Window
‘YE/lavescript/cookie/content2.hi: X
fo) 9@ [0 tenes
4.2.4.2
‘Changing the Content of a Specific Element In Wi
indow
We can change the
content of various el
lem
chore ee content of a specific element of a window sialon
e element you can use ‘id’ .
. attribute. Ther
JavaScript. After getting the access of the el ae
element as given below. =a
such
You need to previa enn division, table etc, if you want
'¢ unique id to that element. To provide thé! id(pion: Ss Sotbthg Language (MSSTE 14 Cookies and Browser Data
‘& JavaSerpt code to change the content of a specific element in window.
leqrigt ope = Textjavecript”>
fonction changeContent() {
document getElementByld("para’) innerHTML= "After clicking the Button: New Content";
‘
Click to change the content of paragraph
“pare’> Before Clicking the Button: Old Content
| Pessveasigteostiel contents X SS
- > Ee O@ fleyfe: ~Ow »
Click to change the content of paragraph
(Ghange conte |
‘After clicking the Button: New ContentCookies and Boren
m™ 4.2.5, we create a paragraph by using
tag with ID “para”. This ‘ID’ is used to get the paragraph Cony
Symcrigt By passing the ID ‘para’ to document.getElementBylD(“para”) function. On that control we Use innern,
\
srry which allocates the new content to the paragraph. Thus only paragraph content Is changed Instead of a
werrsaes
Closing the Window
Howto close a window? Explain with example.
Y the which
“The close) function is used to close the window. By using lose) functions we can only close the Which we open
the help of opent) function.
Program 4.2.6 : Write a JavaScript code to close a window.
Solution :
e 1® files/// “yD
Click to scroll window
[
weenAEfavascript/ec
1 Side Scripting Language (MSBTE) 418 Cookies and Browser Data
hae x i
i ----Window is Scrolled. |
In Program 4.2.7, statement ‘scrollTo(0, 200);'is used to scroll the document in the web page ver
200 pixels, as we mentioned the y coordinate 200,
42.6.2 scrollBy()
The scrollBy() function is used to scroll a specified distance in pixel multiple times.The scrollBy() function scrolls the
document in the webpage by the specified number of pixels.
‘Syntax :
fiindow.scrollBy(xnum, ynum) 7
Parameters :
xnum : it specifies the number of pixels to scroll by, along the x-axis (horizontal). Positive values will scroll the
‘webpage to the right, while negative values will scroll the webpage to the left.
‘ynum :it specifies the number of pixels to scroll by, along the y-axis (vertical). Positive values will scroll the webpageto
bottom, while negative values scroll to top.
Program 4.2.8 : Write a JavaScript code to scroll a window by specified pixels.
|
Click the button to op’
"openWindows()" value= "Open Windows" >
Output :
|< [am > toy 7
> @\|oe
Click the button to open multiple windows.
FG: Mozilla Fiz, — sal
+ @ Mozilla Fir, — FO =
_Mozil
(Open Windows |
'n Program 4.2.20, the statement window.’pen{"",™, "width=200,height=100")rs used within for loop to open mut?
windows at the same time,
4.2.8 Creating a Webpage in New Window
.Q. “How to create a webpage in new window? Explain with example,
We can create a webpage in new window with the help of write function of window object. We can provi
Content of the webpage inside the write() function. The html code as a string parameter is passed to the write) func
We can provide tags such aschtml>,
, , ,
,
, , , etc. The write function is capable”
recognizing the html content and it will convert the html passed as string to the webpage.4-22 Cookies and Browser Data
wo ‘Side Scripting Language (MSBTE)
program 42.11 Wee a JavaScript code to create a webpago in new window.
solution:
fzhiml>
eseript type = "text/javascript">
function openWindow() {
‘yar new_Wwin = window.open("New Window","","width=300,height=300");
new. win.document.write("");
new_win.document.write("");
new_win.document.write("");
new swin.document.wvrite("New Window");
new_win.document.write("");
‘new_win.document.write("");
new. win,document.write("");
new win.documentwrite("
Welcome to the New Window.
");
‘new twin.document.write("
This Webpage is created in New Window.
");
new_win.document.write("");
new_win.documentwrite(""); :
; j
g : as
‘ ose ae
; Bees
Click to open a new window
'
/
Click to get The Path of the Web Pagec/p>
pls has
aeCookies and Browser Data.
ent Sido Sorinting Language (MSBTE) 4-20
alue = "Get the Path” onclick = "displayFunetion()3"/>
Click to get The Path of Fthe Web Page
| (camera)
| The webpage is located at: /pathname-html
In Program 4.2.14, path name is provided by ‘window. location.pathname’.
Program 4.2.15 : Write a JavaScript code to get the Hostname.
function iepagrunciong
document, getElementByld( a
|"+window location. hostname;
‘p> Click to get The Host of the Web Page
input type = *button’ value = "Get the Host" onclick = "displayFunction()s"/> :
SP. id = "para'> Cookies and Brows
Oa
Cllont Sido Soripting Language (MSE.
Output :
ane Tae si
EB locate: X | ea! call
<€7e¢ 0 localhost/hostname.html
Click to get The Host of the Web Page
| [[Get the Host
In Program 4.2.15, ‘wi ‘
indow.location.hostname;‘gives the host name. here we are using local host to run the script,
Program 4.2.16 : Write a JavaSoript code to get the Protocol.ys ‘Serpting Language (MSBTE) 430 Cookies and Browser Data
tocalbs x loca
locas x | +
| € > S| O tocathost/protocot.htmi
‘
Click to get The protocol of the Web Page
{Get the protocol |
The protocol of the webpage is: http:
tn Program 42.6, ‘window:location.protocol‘gives the protocol name. Here we are using local host so that we are
getting the http protocol. If we run a simple html file in a browser, then we will get the file protocol.
4212.2 History
‘The window.history object contains the record of URLs visited by the user within a browser window. The history object
isa partof the window object so that itis accessed by window.history,
is used to return the number of URLs in the list of history.
Methods :
‘back{): itis used to load the previous URL in the history list.
forward() :it is used to load the next URLin the history list.
0f)zitis used to load a specific URL from the history list.
Program 4.2.17 : Write a JavaScript code to get a history list.Cookies and Browse
bee)
ss "ne) 2 onslick
Output :
In Program 4.2.27, statement ‘window. history.back/);is used to get previous URL from history list and ‘window.history,forwords
sed to get the next URL from history list.
gad