Web Page Design Using JavaScript.docx

上传人:b****8 文档编号:10565825 上传时间:2023-02-21 格式:DOCX 页数:19 大小:29.16KB
下载 相关 举报
Web Page Design Using JavaScript.docx_第1页
第1页 / 共19页
Web Page Design Using JavaScript.docx_第2页
第2页 / 共19页
Web Page Design Using JavaScript.docx_第3页
第3页 / 共19页
Web Page Design Using JavaScript.docx_第4页
第4页 / 共19页
Web Page Design Using JavaScript.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

Web Page Design Using JavaScript.docx

《Web Page Design Using JavaScript.docx》由会员分享,可在线阅读,更多相关《Web Page Design Using JavaScript.docx(19页珍藏版)》请在冰豆网上搜索。

Web Page Design Using JavaScript.docx

WebPageDesignUsingJavaScript

外文原文

Source:

WebPageDesignUsingJavaScript

THEBASICS

JAVASCRIPTusesasubsetoftheprogramminglanguageJAVAtoprovideahighlevelofinteractivityonawebpage.JavaScriptsarestoredwithinanHTMLdocumentandareinterpretedbythewebbrowser.

JavaScriptsmaybelocatedwithintheHTMLcodeatthepointinthepagewheretheyaretoappearonthescreenortheymaybewrittenusingfunctions.FunctionsaresmallsubprogramsthatarestoredbetweentheheadtagsofanHTMLdocumentandarecalledontobeexecutedwhenaparticulareventoccurs.

WhetherthescriptisstoredbetweentheheadtagsorwithinthebodyoftheHTMLdocument,itmustbeenclosedinscripttags.Also,asetofHTMLcommenttagsaretypicallyusedinsidethescripttagssothatolderbrowsersthatdonotsupportJavaScriptwillignorethescriptandcontinuetoprocessthepagewithouterrors.

Followingisanexampleofthescriptandcommenttags:

--

IncludeJavaScriptCodeHere

//-->

BeawarethatJavaScriptiscasesensitive...thedifferencebetweenaworkingscriptandanerrormessagecanbeonecapitalletter.

ALERTBOXES

TopopupanalertboxincludethefollowinglineofcodeinsideofscripttagsinthebodyofyourHTMLdocument.Pleasenotethattheprocessingofthepagewillstopuntiltheviewerrespondstothealertbox.

alert("Placethetexttobedisplayedinthealertboxbetweenthesequotes.")

Othertypesofpre-madedialogboxesareavailablesuchasthepromptandconfirmboxes.InordertotakefulladvantageofthefeaturesofthesedialogboxesyoumustwritemoreJavaScriptcodewhichcanusethevaluesthatarereturnedbythedialogboxes.

Thefollowingstatementswillpopupadialogboxthatrequiresayesornoanswer(OKorCancel).IftheanswerisOKthenthevariablenamedanswerhasavalueoftrueandiftheanswerisCancelthenthevariablenamedanswerhasavalueoffalse.YoucanthenuseanifstatementintheJavaScriptcodetorespondappropriately.

varanswer=confirm("Areyousureyouwanttoquit?

")

if(answer==true)

{

window.close()

}

Thefollowingcodewillpopupadialogboxthataskstheusertoentersomesortofinformation.IftheuserclicksOKtheinformationtheyenteredisstoredinthevariable.Thesecondsetofquotationmarksinsideofthepromptstatementmakethecontentsofthetextboxblankwhenthedialogboxisdisplayed.

varresponse=prompt("Whatisyourname?

","")

document.write("Hello"+response+"!

")

Noticethatinthelasttwoexamplesthewindowanddocumentobjectswereused.Windowreferstothebrowserwindowanddocumentreferstothepagebeingdisplayed.Theuseofadotafterthenameoftheobjectallowsactionstobeperformedonthatobjectorpropertiesofthatobjecttobemodified.Inthisnextexample,thenavigatorobjectisreferencedinordertodisplaythebrowsernameandversion.

alert("Youareusing"+navigator.appName+"version"+navigator.appVersion+".")

POP-UPWINDOWS

AnadditionalbrowserwindowmaybeopenedusingasimpleJavaScript.Theopenmethodcontainsthreepartsasinthefollowingexample:

thenameofthedocumentorurlofthewebsitetobedisplayedinthenewwindow,thenamethatmaybeusedtorefertothebrowserwindow(requiresmorecodethanisshownhere),andthepropertiesofthenewwindow.Pleasenotethatthepropertiesarealllistedinonesetofquotationmarksandareseparatedbycommas.

open("myfile.html","mywin","height=200,width=200,titlebar=false")

Thefollowingpropertiesmaybeusedtocontroltheappearanceofthenewwindow:

Table1-1propertiesmaybeusedtocontroltheappearanceofthenewwindow

Feature

Example

Description

height

height=200

determinestheheightofthenewwindowinpixels

width

width=200

determinesthewidthofthenewwindowinpixels

titlebar

titlebar=false

removesthetitlebarfromthenewwindow

location

location

includestheurl/addresstextboxinthenewwindow

menubar

menubar

includesamenubarinthenewwindow

resize

resize=off

makesthenewwindowafixedsize

scrollbars

scrollbars

addsscrollbarstothenewwindow

status

status

includesastatusbarforthenewwindow

toolbar

toolbar=yes

addsatoolbarforthenewwindow

WRITINGFUNCTIONS

FunctionsaresmallsubprogramsthatarelocatedwithinscripttagsbetweentheheadtagsofanHTMLdocument.FunctionsareexecutedwhentheyarecalledbynamefromaneventhandlerwithinthebodyofanHTMLdocument.

Thebasicstructureofafunctionisasfollows:

functionNameOfFunction()

{

IncludeJavaScriptCodeHere

}

EVENTHANDLERS

Thefollowingexampledemonstratestheuseofeventhandleronclickaswellastheuseofstylestocontroltheappearanceofbuttons.Notethatinsteadofusingtype=submitforthebuttonthecodesimplysaystype=button.Copyandpastethisentiresetofcodeintoanewdocumentandtestitout.

Sample

#bigbutton{background-color:

yellow;font-family:

arial;color:

blue;

font-size:

18px;height:

50px;border-width:

0.2cm;border-color:

red}

"

onclick="window.location=''">

Changingthecodeforthebuttontoreadonclick="myfunction()"willresultinexactlythesamethingasthepreviousexampleifthefollowingfunctionisincludedinascriptbetweentheheadtags.Typically,youwouldwriteafunctiononlyiftheeventrequiredmorethanonethingtohappen.

functionmyfunction()

{

window.location=""

}

Thefollowingaresomeoftheeventhandlersthatexistinjavascript:

Table1-2eventhandlersthatexistinjavascript

onfocus

onblur

onselect

onchange

onsubmit

onclick

onmouseover

onmouseout

onload

onunload

onabort

onerror

onreset

onkeypress

onkeyup

onmousedown

onmousemove

onmouseup

onmove

onresize

POP-UPMENUS

Pop-UpMenuscanbequicklycreatedbyusingtheselecttagasitwasusedinformstocreateadropdownlist.Setthevalueofeachoftheoptionsintheselecttagtotheurlofthenewpagetobedisplayed.Usetheonchangeeventhandlertosetthelocationofthewindowtotheselectedvalueinthedropdownlist.Forexample,iftheformisnamedmyform,theselecttagisnamedmychoices,andthevalueofeachoptionisaurlthenthestatementwindow.location=document.myform.mychoices.valuewilltakeyoutothenewpagethatwasselectedfromthedropdownlist.

Bydefaultonlyoneiteminalistisdisplayedbyaselectstatementuntiltheviewerclicksonthedownarrowtoexposetherestofthelist.Todisplaymorethatoneitematatime(andcreateatextboxwithaverticalscrollbar)includethesizeattributeintheselecttag.Forexample,size=5willdisplaythefirstfiveitemsinthelistandaddaverticalscrollbartotheboxiftherearemorethan5itemsinthelist.

MOUSEOVERS

Amouseoverreferstotheeffectthatoccurswhenthepropertiesofanobjectarechangedifthemouseispositionedoverthetopoftheobjectandthenagainifthemouseisremovedfromtheobject.Thequickestwaytogenerateamouseoveristousetheonmouseoverandonmouseouteventhandlersinaformofin-linestyle.

VisitthestylesectionoftheDHTMLpageofthiswebsitetoseeanexampleofmouseoversusedwithtextasanin-linestyle.Anystylepropertythatappliestoaparticularobjectcanbechangedastheresultofamouseover.

Performingmouseoverswithagraphicisnotmuchdifferentthanwithtext.Whenthedesiredeventoccurs(onmouseover,onmouseout)changethesourceofthegraphicasintheexamplethatfollows:

SCROLLINGTEXT

SincethemarqueetagisonlysupportedbyInternetExploreritisagoodideatoavoiditasmuchaspossibleanduseaJavaScripttogeneratescrollingtextinstead.WiththisJavaScriptitisalsoquiteeasytoplacethescrollingtextonthestatusbarinsteadofinthedocumentitselfbyusingwindow.statusasthedestinationforthemessage.Thefollowingfunctionwillgenerateascrollingmessageinatextboxnamedmymessageboxwhichispartofaformnamedmyform.Theeventhandleronloadmustalsobeusedinthebodytagtocallthefunctionwhenthepageloads.

varmessage="Thisisatest...         "

varposition=0

functionmymessage()

{

document.myform.mymessagebox.value=

   message.substring(position,message.length)+

   message.substring(0,position)

position=position+1

if(position>message.length)

{

position=0

}

window.setTimeout("mymessage()",300)

}

DATESANDTIMES

Datesandtimesareoftendisplayedonwebpagestoindicatewhenapagewaslastupdated,whenapagewasloaded,ortodisplayacountdowntoaparticularevent.Displayingthedateandtimeofthelastupdateisagoodpracticetogetintoforallofyourpagesbecausefrequentupdatesareonesignofaqualitysite.Thedate/timestampletstheviewerknowhowrecenttheinformationisandthereforeprovidesoneindicationofvalidity.Todisplaythedateandtimeofthelastupdate(thelasttimethedocumentwassaved)usethefollowingonelineinsideofscripttags:

document.write("Thispagelastupdated"+document.lastModified)

TodisplaythecurrenttimeanddateonawebpageyoumustdeclareavariableoftypeDate(varnow=newDate).Thevariablecanthenbeusedtoaccessvariouspartsofthedateandtimeincludingday

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 经济学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1