Archive for the ‘Javascript’ Category

Javascript: Print Page button

Tuesday, January 6th, 2009

here’s a very simple javascript to place a link on a webpage to facilitate the user’s printing of that page:

simply paste the code below where you want the button to appear:

<SCRIPT LANGUAGE=”JavaScript”>
<!– Begin
if (window.print) {
document.write(‘<form>Click Here To ‘
+ ‘<input type=button name=print value=”Print” ‘
+ ‘onClick=”javascript:window.print()”> This Page!</form>’);
}
// End –>
</script>

Javascript: Display Random Text from a list

Monday, January 5th, 2009

Did you ever want to display a random text from a pre-populated list?  Maybe you wanted to randomly display a client testimonial from a list of 10… well, here’s the script you need.

place the following code in the head of your document (somewhere between the <head> and </head> tags):
<script type=”text/javascript” src=”../scripts/rotate.js”></script>

now, place the following code in the body of your document, where you want the random text to be displayed:
<script language=”JavaScript”>
<!–
document.write(r_text[i]);
–>
</script>

then, create a javascript document named rotate.js with the following code:
var r_text = new Array ();
r_text[0] = “random text 1″;
r_text[1] = “random text 2″;
r_text[2] = “random text 3″;
r_text[3] = “random text 4″;
r_text[4] = “random text 5″;
r_text[5] = “random text 6″;
r_text[6] = “random text 7″;
r_text[7] = “random text 8″;
r_text[8] = “random text 9″;
r_text[9] = “random text 10″;

var i = Math.floor((r_text.length)*Math.random())

and edit the texts.  You can add or delete as many lines of random texts as you wish and they will automatically be included.  Then save it in a subdirectory named “scripts”.

That’s all you need!

Creating a simple javascript Popup Div

Monday, October 20th, 2008

Sometimes you want to create a div in your document that opens a pop-up window that shows another webpage, but that isn’t actually another browser window.  The code below will do just that for you in a simple manner.  You can change the page that shows in the popup by simply changing the “src” from “http://google.com” to whatever page you want to display.

place the following code in the body of your html page:

<div class=”popUp”>
<iframe src=”http://google.com”></iframe><br>
<button onclick=”this.parentNode.parentNode.removeChild(this.parentNode);”>
Close
</button>
</div>

place the following code either in your css style sheet, or between <style></style> tags in the head of your html document:

.popUp { position: absolute; top: 1000px; left: 200px; text-align: center; padding: 5px; border: 1px solid black; background: white; }

place the following code in the head of your document:

<script>
function createPopUp(popUpCode) { var div = document.createElement(‘div’); div.innerHTML = popUpCode; document.body.appendChild(div.firstChild); }
</script>