\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{binca} \pdfinfo{ /Title (javascript.pdf) /Creator (Cheatography) /Author (binca) /Subject (JavaScript Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{3AA32A} \definecolor{LightBackground}{HTML}{F2F9F1} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{JavaScript Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{binca} via \textcolor{DarkBackground}{\uline{cheatography.com/44948/cs/13428/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}binca \\ \uline{cheatography.com/binca} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 9th November, 2017.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{About}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Most common client-side scripting language in use. Originally designed by Netscape in 95 and called LiveScript.} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Use for for reading and understanding scripts from the target systems.} \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Can be used to write attacks against target systems.} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{In web pages is can be inline with HTML via {\bf{\textless{}script\textgreater{}}} tags, as a part of an HTML item {\bf{\textless{}img src="link" onload="javascript;"\textgreater{}}}, or loaded from an external document {\bf{\textless{}script \seqsplit{src=http://evil.agent/bad}.js\textgreater{}}}} \tn % Row Count 12 (+ 5) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{It is an object-oriented language.} \tn % Row Count 13 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.6 cm} x{6.4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Control Statements}} \tn % Row 0 \SetRowColor{LightBackground} while loop & Runs a block of code until a condition is met \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} for loop & Runs for a set number of times \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Variables}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Any type of data can be assigned to a variable without concern.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Declaration: {\bf{var x;}}} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Variables can be assigned at declaration or later. {\bf{var x="string";}} or {\bf{x="string";}}} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{If a variable is re-declared after a value is assigned to it, the original value is still assigned.} \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Global variables}} are those declared outside of functions and are accessible everywhere.} \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Instance variables}} are those declared within a function and are exclusive to the function.} \tn % Row Count 11 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Functions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Functions can be declared anywhere within the page, but it is safest to declare in the \textless{}HEAD\textgreater{} to ensure they are loaded before being called.} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{function name(var1, var2) \{ some code \}}}} \tn % Row Count 4 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{To return data from a function use {\bf{return var;}} statement within the function.} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Call a function using {\bf{function\_name()}}} \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.32 cm} x{5.68 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Events}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{onload}} & Page of item is finished loading \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{onunload}} & User leaves the page the script is on \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{onerror}} & An error occurs loading page or item \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} {\bf{onclick}} & Item is clicked on with mouse \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} {\bf{onsubmit}} & The form is submitted \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} {\bf{onfocus}} & The item receives focus \tn % Row Count 11 (+ 1) % Row 6 \SetRowColor{LightBackground} {\bf{onblur}} & The item loses focus \tn % Row Count 12 (+ 1) % Row 7 \SetRowColor{white} {\bf{onchange}} & Content of field changes \tn % Row Count 14 (+ 2) % Row 8 \SetRowColor{LightBackground} {\bf{onmouseover}} & The mouse is hovering over item \tn % Row Count 16 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Every item in a page has a series of associated events. The event calls a function.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.12 cm} x{4.88 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Events within Attacks}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{onload}} & Change content of page after it loads \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{onunload}} & Launch pop-under window to retain control of a zombie browser \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} {\bf{onsubmit}} & Change form values so the transaction is one of the attacker's choosing. \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} {\bf{onfocus}} & Send HTTP request to attacker's web server to reveal which controls the user is selecting. \tn % Row Count 12 (+ 4) % Row 4 \SetRowColor{LightBackground} {\bf{onerror}} & Used within web scanners injected via XSS to determine a resource does not exist. Usefule when port scanning a network using JavaScript. \tn % Row Count 18 (+ 6) % Row 5 \SetRowColor{white} {\bf{onclick}} & Change where a link points without the user knowing. \tn % Row Count 21 (+ 3) % Row 6 \SetRowColor{LightBackground} {\bf{onmouseover}}\{\{nobreak\}\} & Track the movement of the mouse across a page. \tn % Row Count 23 (+ 2) % Row 7 \SetRowColor{white} {\bf{onblur}} & Send the contents of a form field to an attacker. \tn % Row Count 26 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Document Object Model (DOM)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Provides standard interface tot he document allowing scripts to dynamically access and update content, structure, or style of the page. \{\{nl\}\} Doc referenced is either HTML or XML} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{DOM provides native objects to access various items of interest: \{\{nl\}\}{\bf{document.forms{[}0{]}}} refers to 1st form on page \{\{nl\}\}{\bf{document.write("string")}} write string to the page \{\{nl\}\}{\bf{document.write(document.cookie)}} will write value of the page's cookies to the page} \tn % Row Count 10 (+ 6) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Form object is used to access a specific form \{\{nl\}\} {\bf{form.action={[}URL{]}}} sets the forms action to the URL allowing for redirecting the browser to another page \{\{nl\}\} {\bf{form.submit()}} will submit form} \tn % Row Count 15 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{DOM Nodes}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Viewed as a tree the HTML tag is the root and has two children {\bf{\textless{}HEAD\textgreater{}}} and {\bf{\textless{}BODY\textgreater{}}}. Each other them have children and so forth.} \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Object Methods and Properties}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Objects have to be initialized instead of being assigned to a variable; {\bf{var string=new String();}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Objects have properties, attributes of the object, and methods, which are actions performed on the object.} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Devs can create their own objects.} \tn % Row Count 6 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{When referring to a property of an object, we use the format {\bf{object.property}}, such as {\bf{document.referrer}}.} \tn % Row Count 9 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Calling a method is similar but also requires {\bf{( )}} with values determined by the method.} \tn % Row Count 11 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.672 cm} x{2.964 cm} x{2.964 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Objects and Associated Properties and Methods}} \tn % Row 0 \SetRowColor{LightBackground} Object Type & Method & Property \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{String}} & {\bf{split()}} parses the string & length returns size \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Date}} & {\bf{getTime()}} returns current time \{\{nl\}\}{\bf{getMonth()}} returns current month & \tn % Row Count 10 (+ 6) % Row 3 \SetRowColor{white} {\bf{Array}} & {\bf{join()}} joins the elements in the array \{\{nl\}\}{\bf{sort()}} sorts the array & \tn % Row Count 15 (+ 5) % Row 4 \SetRowColor{LightBackground} {\bf{Window}} & {\bf{open()}} creates a new browser window\{\{nl\}\}{\bf{alert()}} pops up a dialog box & \tn % Row Count 21 (+ 6) % Row 5 \SetRowColor{white} {\bf{Document}}\{\{nobreak\}\} & {\bf{write()}} writes content to the page \{\{nl\}\} {\bf{referrer()}} returns referring URL & \tn % Row Count 27 (+ 6) % Row 6 \SetRowColor{LightBackground} {\bf{Location}} & {\bf{reload()}} reloads doc & {\bf{port()}} returns the port of the current page \tn % Row Count 31 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{1.672 cm} x{2.964 cm} x{2.964 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Objects and Associated Properties and Methods (cont)}} \tn % Row 7 \SetRowColor{LightBackground} {\bf{History}} & {\bf{back()}} is the same as the back button & Length returns history item count \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Selecting and Changing Content}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Scripts can find specific content by walking the DOM.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{The script can read the item's attributes and associated items such as text.} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{The script can then rewrite the item.} \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{function counttags(tag) \{ \newline count = \seqsplit{document.getElementsByTagName(tag).length} \newline return count \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Interacting with Cookies}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{strCookie=document.cookie}} returns only the name=value pairs} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Parsing the cookie takes a little work.\{\{nl\}\} 1. Parse to split each name=value pair. {\bf{var \seqsplit{arrValues=document.cookie}.split(';');}} \{\{nl\}\} 2. Loop through each pair and split on the {\bf{=}}. (4:22)} \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Setting cookies only requires 3-4 parameters: {\bf{A cookie name and value pair}}\{\{nl\}\}{\bf{Expiration time for the cookie ad URI path that is able to access it}}\{\{nl\}\}Data NOT required for session cookies} \tn % Row Count 10 (+ 4) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{document.cookie="userid=person;} expires=Wed, 1-Nov-2017; path=/";} \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}