Script Notes

Anchors
Cancel Events Default Action
Change a number to a string
Close Window Error
Counting Elements in a Document
Disable Context Menu
Document Location
Document.ondblclick
Document Size
Elements Content
Elements Location
Make Homepage
Mouse Events
Passing arguments via timeout
Preventing a Document From Being Cached
Pixels to inches
Security Warning
Screen Size
Scroll Position
Scrollbar Width
Short Path
Unselectable
Window Size

Anchors

Go directly to the various tags in the page

<script type="text/javascript">
<!--
function travelTo(el){
document.getElementById(el).scrollIntoView(true) // true = top of page, false = bottom of page
}
// -->
</script>

<a href="#null" onclick="travelTo('anchor1')">Anchor 1</a>

<div id="anchor1"></div>Anchor 1

Or

<script type="text/javascript">
<!--
function travelTo(el){
here=document.getElementById(el).offsetTop
window.scrollTo(0,here)
}
// -->
</script>

<a href="#null" onclick="travelTo('anchor1')">Anchor 1</a>

<div id="anchor1"></div>Anchor 1


Cancel events default action

Test Link

IE=document.all

tag_id=(IE?event.srcElement.id:e.target.parentNode.id) // specific IDs
if(tag_id=="yourid"){
window.event.returnValue=false
}

tag_id=(IE?event.srcElement.tagName:e.target.nodeName) // specific TAGS
if(tag_id=="A"){
window.event.returnValue=false
}

function stop(){ // prevent use of mousewheel
return false;
}

document.onmousewheel=stop;


Change a number to a string

num = 30
num = num.toString()


Close Window Error

The webpage you are viewing is trying to close the window.
Do you want to close this window?

Add the following to a function

window.opener = top;
window.close()


Counting Elements in a Document

The getElementsByTagName() method retrieves all HTML elements of a specific tag from the document and returns an array of elements.

document.getElementsByTagName("P").length

View


Disable Context Menu

Add onContextMenu="return false"to the opening BODY tag.


Document Location

Shows the location of the document.

<script type="text/javascript">
<!--
document.write(document.location)
//-->
</script>


When loading a page in Mozilla using Javascript add return false to the onclick event

function test(){
window.location = "page.htm"
}

<a href="#null" onclick="test() ; return false">test</a>


Document.ondblclick


Document Size

document.body.scrollWidth
document.body.scrollHeight

View


Elements Content

document.getElementById("id").innerHTML

View


Elements Location

document.getElementById('ID').offsetTop
document.getElementById('ID').offsetLeft

<span id="ID">The Element</span>

View


Make Homepage

<span style="cursor:hand; color:blue; text-decoration:underline" onclick="this.style.behavior='url(#default#homepage)'; this.setHomePage('http://www.huntingground.freeserve.co.uk')">Make Homepage</span>


Mouse Events

OnClick event

<A href="#null" onClick="alert('This is a\n\n'+ event.type+'\n\nEvent')">OnClick event</A>

Double Click event

<A href="#null" ondblClick="alert('This is a\n\n'+ event.type+'\n\nEvent')">Double Click event</A>

OnMouseOver event

<A href="#null" onMouseover="alert('This is a\n\n'+ event.type+'\n\nEvent')">OnMouseOver event</A>

OnMouseOut event

<A href="#null" onMouseout="alert('This is a\n\n'+ event.type+'\n\nEvent')">OnMouseOut event</A>

OnMouseDown event

<A href="#null" onMousedown="alert('This is a\n\n'+ event.type+'\n\nEvent')">OnMouseDown event</A>

OnMouseUp event

<A href="#null" onMouseup="alert('This is a\n\n'+ event.type+'\n\nEvent')">OnMouseUp event</A>

Script wise

<script type="text/javascript">
<!--
function test_mouse_event(e){
alert(e.type)
}
// -->
</script>

Test Mouse Event Test Mouse Event

You have just done an event type of

Testable events.

onmouseover="test_mouse_event(event)"
onmouseout="test_mouse_event(event)"
onclick="test_mouse_event(event)"
ondblclick="test_mouse_event(event)"
onmousedown="test_mouse_event(event)"
onmouseup="test_mouse_event(event)"


Passing arguments via timeout

<a href="#null" onclick="test1(1,2,3)">Variable</a> <a href="#null" onclick="test1('a','b','c')">String</a>

Test 1 Integer String

function test1(x,y,z){
setTimeout("conclusion("+ x + "," + y + "," + z + ")",1000) // integer only
}

Test 2 Integer String

function test2(x,y,z){
setTimeout("conclusion( ' "+ x + " ',' " + y + " ',' " + z + " ' )",1000); // integer & string
}

Test 3 Integer String

function test3(x,y,z){
setTimeout(function(){conclusion(x,y,z)}, 1000) // integer & string
}

function conclusion(x,y,z){
alert("F2 = "+x+" "+y+" "+z)
}


Preventing a Document From Being Cached

You can prevent a document from being cached by adding the following META tag to the document.

<META HTTP-EQUIV="Expires" CONTENT="0">

Preventing the document from being cached ensures that a fresh copy of the document will always be retrieved from the site, even during the user's current session, regardless of how the user has set the browser's caching options.


Pixels to Inches

Determine position stated in inches into pixels.

Create a dummy marker using a div tag and style attributes.
State the absolute position in inches then use pixelLeft or offsetLeft to convert into pixels.

<div id="dummy" style="position:absolute; left:1in; top:2in"></div>

<A href="javascript:alert('dummy.style.pixelLeft = '+dummy.style.pixelLeft+' pixels\n\ndummy.offsetLeft = '+dummy.offsetLeft+' pixels')">Convert</A>

Convert


Security Warning

Why can't I run scripts on my local machine without getting a security warning?

With SP2 installed the default setting is to block JavaScript from running on the local machine.

Go into Internet Explorer tools menu and select internet options.
Scroll down until you see the Security heading.
Check the "Allow active content to run in files on my computor" checkbox and your code will run.


Screen Size

screen.width
screen.height

View


Scrollbar Width


Scroll Position

document.body.scrollLeft
document.body.scrollTop

View


Short Path

full_path=url
short_path=full_path.substring(full_path.lastIndexOf("\\")+1,full_path.length)

Example:
Full path = C:\My Documents\My Pictures\Sample.jpg
Short path = Sample.jpg


Unselectable

IE only

Apparently if you include unselectable="on" in any HTML tag it renders the contents of that tag .... well ...... unselectable.

Try selecting some of this text.


Window Size

The following examples and may vary depending on which browser you are using to view this page.




location.hostname