Tải bản đầy đủ - 0 (trang)
Hack 37. Control Browser History with iframes

Hack 37. Control Browser History with iframes

Tải bản đầy đủ - 0trang









Codecanalsocreateiframesdynamically,throughtheDOMand

JavaScript,afterthepageisfinishedloading:







functioninitialize(){

vartestFrame=document.createElement("IFRAME");

testFrame.id="testFrame";

testFrame.src="http://www.google.com";

document.body.appendChild(testFrame);

}













Okay,sotherearetwokindsofiframe.Whocares?Well,itturns

outthesetwokindsofiframehavecompletelydifferent

behaviorswhenitcomestohistoryindifferentbrowsers!



BrowserLowdown

Here'sthelowdownforeachkindofbrowser:



InFirefox,iftheiframeisinsidetheHTMLandwasloadedin

thepage,anylocationchangestoitarestoredinthe

browser'shistory.IftheiframewaswrittenintotheDOM

throughJavaScriptafterthepagefinishedloading,no

locationchangesarestoredinthebrowser'shistory.

InInternetExplorer,locationchangesarestoredinthe

browser'shistoryforbothkindsofiframe.

InSafari,locationchangesarenotstoredinthebrowser's

historyforeitherkindofiframe.

Youcanseethisforyourselfinthedemosdiscussedinthenext

section.



BrowserDemos

TwodemoshavebeenprovidedtoillustratehowFirefoxand

InternetExplorerhandlethedifferentkindsofiframe.Inboth

demos,wedynamicallychangetheiframe'slocationbetween

fourdifferentwebsites.



Inthefirstdemo,viewableat

http://codinginparadise.org/projects/tutorials/tale_of_two_iframes/static_i

wearedealingwithaniframethatisintheHTMLonpageload.

Inthiscase,youwillfindthatallofthesesitesareinthe

browser'shistoryinbothFirefoxandIE.Pressthebackand

forwardbuttonswhenthepop-upsaying"Finished"appears,

andyouwillseetheiframe'scontentschangebetweeneachsite.



Intheseconddemo,viewableat

http://codinginparadise.org/projects/tutorials/tale_of_two_iframes/dynam

wearedealingwithadynamicallycreatediframe.Here,youwill

findthatonlytheinitialpageloadisinthebrowser'shistoryin

Firefox,whileallsitesareinthehistoryinIE.



Onesmallfootnoteisthatifyouhaveastaticiframethatis

loadedintheHTML,andthatiframehasasrcvalueinitially(as

in,thisinitialvalueis

notplacedinthebrowser'shistory.Inthiscase,onlysuccessive

changestothatstaticiframeareplacedinthehistory.

Youcanusethespecialbehaviorofthesetwokindsofiframefor

somerealtrickery.First,maketheminvisibleusingCSS.You

canthendecidewhetheryouwantsomethingtoenterthe

historyornot,choosingtheappropriatekindofiframe.Ifyou

areworkingwithaDHTMLapplicationthatusesiframesfor

remotecommunication(detailedat

http://developer.apple.com/internet/webcontent/iframe.html)

insteadofXMLHttpRequest,foroldbrowsercompatibility,knowing

thedifferencebetweenthesetwokindsofiframecanbevery

useful,becauseyoucanchoosewhetherremoteiframe

communicationisplacedinthebrowser'shistoryornot.



Foradiscussionofhowyoucanuseiframestomakethebrowserback

buttonworknormallywithAjaxapplications,checkout"FixtheBrowser

BackButtoninAjaxApplications"[Hack#68].



BradNeuberg







Hack38.SendCookieValuestoaServer

Program



Createcookieswithinthewebapplicationandsend

cookievaluestoaserverwithoutrefreshingthepage.

Acookieisasmallpieceofdatathatawebapplicationcan

storeonauser'smachine.Awebsitecansetoneormore

cookiesusingaSet-Cookieheaderintheserverresponse.The

numberofcookiesthataservercansetandtheirindividual

sizesarerestrictedbasedonthestandardsusedbythefirst

browsermakers,suchasNetscape.



Awebservermaysetnomorethan20cookiesforonebrowser,and

eachcookie'ssizeislimitedto4K.(Veryfewcookiesreachthatsize.)If

youwanttoviewthecookiesinyourFirefoxinstallation,goto

Preferences

Privacy

Cookies

ViewCookies.Mostbrowsers,

includingFirefox,allowtheusertoremovecookies.



IfGoogleorYahoo!,forinstance,hassetacookieonyour

machine,thatbusiness'swebapplicationswillbeabletoread

thecookienameandvaluethenexttimeyougotoitssite.For

example,GooglemaysetauniqueIDonyourmachinesothat

itcanidentifyyouanddisplayyourpreferentialnewsheadlines

whenyouvisittheGoogleNewssite.Theupcomingsectionswill

explainhowcodesetstheaccessibilityoftheinformationthe

cookiesstore.



BakeYourOwnCookie



Thishackallowsausertoenterthenameandvalueofa

cookie.Theapplicationthenusesthisinformationtogeneratea

newcookie.Figure4-13showstheinteractionsthattakeplace

inthishackbetweenthebrowserandtheserver.



Figure4-13.Creating,viewing,andposting

cookiedata



Thesimplestcookiecomprisesanameandvalue,asin

mycookie=uniqueADDAA.Mostcookiesaresetbytheserverusinga

shortstringofdatathatincludesthewebserverpaththatis

linkedtothecookie,aswellasthedateonwhichthecookie

expiresandcanbedeletedbythewebserver:

mycookie=uniqueADDAA;expires=Thu,01Dec200520:35:16GMT;

path=/ajaxhacks;domain=parkerriver.com



Inthishack,theuseralsohastheoptiontoviewexisting

cookiesbyclickingabutton,aswellastosendtheexisting

cookiecollection,includingtheirowncookies,toaserver

component.

Figure4-14showsthewebpageforthisapplication.



Figure4-14.Fireupyourowncookie



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hack 37. Control Browser History with iframes

Tải bản đầy đủ ngay(0 tr)

×