Dec/080
Usefull Javascript functions
Working on some stuff over the last few days I had to dig up all my javascript knowledge since I had to do something in simple javascript and html.
The reason I had to dig things up is because over the last couple of years every time I had a need in doing something the path I would follow would be PHP. I would write a script and more often than not I would be finished with it in a matter of minutes. Well since I wasn’t able to use PHP here I had to look into another way of doing what I wanted.
Below I will explain some function I’ve used, how you use them and why.
Please note that this might not be the best way to solve these problems, but it is the way I solved them. If you have something to say use the comments.
Problem 1: There was a need to be able to pass variables into the address bar so I could load something specific in the content or the user could jump right into a page without needing to navigate the javascript menu sidebar.
The following function reads all query variables from the title bar and splits them into an array and returns the first variable only. If no variables are found then it returns an empty string.
Function getQueryVariable():
function getQueryVariable(variable){
var query = window.location.search.substring(1);
var vars = query.split("&");
for(var i=0;i<vars .length;i++){
var pair = vars[i].split("=");
if(pair[0] == variable){
return pair[1];
}
}
return "";
}
Problem 2: There was a need to trigger a print action in order to print the current page.
The following function does just that, alongside with a print css we print only the content we want.
Function Print():
function Print(){
document.body.offsetHeight;window.print();
}
Problem 3: There was a need to print all pages.
Since the main content of all pages was kept in seperate html files and was loaded using javascript all we have to do here is combine all the html files into a huge string and add it to out page.
The following function does just that. We initialize our XMLHttpRequest this is how we are going to load the files from the array files2print. Then we open each file and append it to the end of a string.
Function PrintAll():
function PrintAll(){
var AJAX = null; // Initialize the AJAX variable.
var all_content = "";
var files2print = ['home.html','page1.html','pag2.html'];
if (window.XMLHttpRequest){ // Are we working with mozilla?
AJAX=new XMLHttpRequest(); // Yes this is mozilla.
}else{ // Not Mozilla, must be IE
AJAX=new ActiveXObject("Microsoft.XMLHTTP");
} //End setup Ajax.
if(AJAX==null){ //If we couldn't initialize Ajax...
alert("Your browser doesn't support AJAX."); //Sorry msg.
}else{
var httpRequest = AJAX;
// Go through the array and create a string with all the html files contents
for ( var i=0, len=file2print.length; i<len ; ++i ){
httpRequest.open('GET', files2print[i], false);
httpRequest.send(null);
all_content = all_content+"<br/>"+httpRequest.responseText;
}
document.getElementById('main-content').innerHTML = all_content;
document.body.offsetHeight;window.print();
}
}
Well this is it if you have any questions don’t hesitate to leave comments.
No comments yet.
Leave a comment
By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution.
No trackbacks yet.

















