Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Jerry's Javascript Library

innerHTML

  • Please log in to reply
No replies to this topic

#1 Roger

Roger

    Skadoosh!

  • Administrator
  • 1222 posts
  • Programming Language:C, PHP
  • Learning:Others

Posted 21 December 2011 - 08:43 AM

This tutorial is split from this thread: http://forum.codecal...571-hi-all.html

Wanna' use session variables or read and write cookies from javascript? Even after the page loaded?

Wanna' use javascript to access a url not in your own domain?

Wanna' easy way to access server-side php files to process information or access files on the server?

Wanna' know the x or y position of an html element or easily change the mouse cursor?


Just copy and paste the javascript code portion of the page below into your html and save the php code as phplib.php on your server. I suggest making a directory on the root named "includes" and putting it there. This javascript references includes/phplib.php So if you don't put it there, change the javascript to reflect that. Leave everything after and including the "?" Just change includes/phplib.php

I think the best way to explain how to use it is by giving you working examples. So if you just copy and paste all the html and javascript from the large code blocks below into a page and throw it on your server so javascript can access the phplib.php you will see working examples of everything. I also give little code examples.

Set the cookie "lastvisit" to "today" and remember the cookie for a month:
javascript: var today= new Date(); setserver('lastvisit', today.getTime(), 'c', (3600*24*30));

3600 seconds in an hour; 24 hours in a day; 30 days in a month.

Read the cookie "lastvisit" to see when your page visitor last looked at your page
javascript: server('lastvisit', 'c');

Read the a session variable named 'shoppingcart' and add 'mybook' to it:
javascript: setserver('shoppingcart', (server('shoppingcart', 'v')+'; mybook'), 'v', 0);

Get it? 'c' is for cookie, 'v' is for session variable. Zero time, since we don't need time for a session variable. The visitor can load another page while shopping around and javascript: server('shoppingcart', 'v') will always contain the items in the shopping cart.
javascript: server('shoppingcart', 'v')


You want the mouse to change to a pointer when passing over an image:
<img src="myimage.gif" onmouseover="mouse('p')" onmouseout="mouse('d')>

Get it? 'p' is for pointer; 'd' is for default; use the actual mouse name for any other

Display the contents of a web page that is not on your server from within your web page:
<input id="foreignpage" value="http://">
<span onclick="document.getElementById('foreignpagecontents').innerHTML= urlgetxd(document.getElementById('foreignpage').value, 20, 0)" onmouseover="mouse('p')" onmouseout="mouse('d')">(see page)</span>
<div id="foreignpagecontents" style="border: thin blue solid; margin: 20px; padding: 10px;"></div>

Send input to your server for some time intensive processing and let the viewer continue to browse:
javascript: urlgetb('yourphpcode_onyourserver.php', 'showresults'); function showresults(a){document.getElementById('results').innerHTML= unescape(a);}
<div id="results"></div>

Have fun!

Jerry


<div style="margin: 30px;">

<span onmouseover="mouse('p')" onmouseout="mouse('d')">
Change the mouse to a pointer when passing over an image or text.
</span><br>
<br>

Get it?  'p' is for pointer; 'd' is for default;  use the actual mouse name for any other<br>
<br>

<span onclick="var today= new Date(); setserver('lastvisit', today.getTime(), 'c', (3600*24*30));" onmouseover="mouse('p')" onmouseout="mouse('d')">
Set the cookie "lastvisit" to "today" and remember the cookie for a month
</span><br>
<br>

<span onclick="this.innerHTML= server('lastvisit', 'c');" onmouseover="mouse('p')" onmouseout="mouse('d')">
Read the cookie "lastvisit" to see when your page visitor last looked at your page.
</span><br>
<br>

<span onclick="setserver('shoppingcart', (server('shoppingcart', 'v')+'; mybook'), 'v', 0, 0);" onmouseover="mouse('p')" onmouseout="mouse('d')">
Add 'mybook' to a session variable named 'shoppingcart':
</span><br>
<br>

<span onclick="this.innerHTML= server('shoppingcart', 'v');" onmouseover="mouse('p')" onmouseout="mouse('d')">
Read the session variable shoppingcart.
</span><br>
<br>

Get it?  'c' is for cookie,  'v' is for session variable.<br>
<br>

The visitor can load another page while shopping around and javascript: server('shoppingcart', 'v');  will always contain the items in the shopping cart.<br>
<br>

Display the contents of a web page that is not on your server from within your web page:<br>
<input id="foreignpage" value="http://">
<span onclick="document.getElementById('foreignpagecontents').innerHTML= urlgetxd(document.getElementById('foreignpage').value, 20, 0)" onmouseover="mouse('p')" onmouseout="mouse('d')">(see page)</span>
<div id="foreignpagecontents" style="border: thin blue solid; margin: 20px; padding: 10px;"></div>
<br>

Send input to your server for some time intensive processing and let the viewer continue to browse:<br>
This won't work unless you have a file yourphpcode_onyourserver.php and it echos the results when it is done.<br> 
javascript: urlgetb('yourphpcode_onyourserver.php', 'showresults'); function showresults(a){document.getElementById('results').innerHTML= unescape(a);}
<div id="results"></div>
</div>

<script type="text/javascript">
// Jerry's javascript library

function setserver(name, value, cookorvari, time){
	urlgetb(('includes/phplib.php?cookie=w'+cookorvari+'&n='+name+'&v='+value+'&t='+time), 'donothing');
}

function server(name, cookorvari){
	return urlget('includes/phplib.php?cookie=r'+cookorvari+'&n='+name);
}

function urlgetxd(url, timeout, post){
	return urlget('includes/phplib.php?xdomain='+escape(url)+'&t='+timeout+'&p='+post);
}

function getypos(obj){
	var y= 0
	while(obj){
		y= y+obj.offsetTop;
		obj= obj.offsetParent;
	}
	return y;
}

function getxpos(obj){
	var x= 0
	while(obj){
		x= x+obj.offsetLeft;
		obj= obj.offsetParent;
	}
	return x;
}

function enterkey(a){
	if (window.event){key= window.event.keyCode;}
	else if (a){key= a.which;}
	else{key= 0;}
	if (key==13){return true;}
	else{return false;}
}

function mouse(mode){
	if (mode=='p'){mode= 'pointer';}
	if (mode=='d'){mode= 'default';}
	document.body.style.cursor= mode; 
}

function urlget(where){
	if (window.XMLHttpRequest){
		a= new XMLHttpRequest();
	}else{
		a= new ActiveXObject("Microsoft.XMLHTTP");
	}
	a.open('GET', where, false);
	a.send();
	return a.responseText;
}

function urlgetb(where, dowith){
	// always use unescape() in function dowith
	if (window.XMLHttpRequest){
		pagea= new XMLHttpRequest();
	}else{
		pagea= new ActiveXObject("Microsoft.XMLHTTP");
	}
	pagea.onreadystatechange= function(){
		if (pagea.readyState==4 && dowith!='donothing'){
			var resp= dowith+'("'+escape(pagea.responseText)+'")';
			eval(resp);
		}
	}
	pagea.open('GET', where, true);
	pagea.send();
}
</script>

<?php
// Jerry's php library

session_start();

if (strpos($_SERVER['QUERY_STRING'], 'xdomain=')===0){
	//?xdomain='+escape(url)+'&t='+timeout+'&p='+post
	$t= (trim($_GET['t'])+0); // ensures php sees $t as a number
	$output= geturl(urldecode($_GET['xdomain']), 0, $t, urldecode($_GET['p']), 0); 
	if ($output!=false){echo $output;}else{echo 'fail';}
	exit();
}

if (strpos($_SERVER['QUERY_STRING'], 'cookie=')===0){
	//?cookie=w'+cmd+'&n='+name+'&v='+cookorvari+'&t='+time
	$t= (trim($_GET['t'])+0); // ensures php sees $t as a number
	if ($t==0){$time= time()-(3600*24*30);}else{$time= time()+$t;}
	if (strtolower($_GET['cookie'])=='wc'){setcookie($_GET['n'], $_GET['v'], $time);echo 'done';}
	else if (strtolower($_GET['cookie'])=='wv'){$_SESSION[$_GET['n']]= $_GET['v'];echo 'done';}
	else if (strtolower($_GET['cookie'])=='rc'){echo $_COOKIE[$_GET['n']];}
	else if (strtolower($_GET['cookie'])=='rv'){echo $_SESSION[$_GET['n']];}
	else{echo 'fail';}
	exit();
}

function geturl($url, $agent, $timeout, $post, $cookiejar){
	$page= false;
	if (strlen($url)>7){
		if (strtolower(substr($url, 0, 7))=='http://'){
			$a= ' (Jerrybot; http://www.botproject.jerrywickey.net/ ) ';
			if (strlen($agent)<5){$agent= $a;}
			if (strlen($timeout)<1){$timeout= 6;}
			if (strlen($post)<5){$post= false;}
			if (strlen($cookiejar)<5){$cookiejar= false;}
			$curl= curl_init();
			curl_setopt($curl, CURLOPT_URL, $url);
			curl_setopt($curl, CURLOPT_USERAGENT, $agent);  
			curl_setopt($curl, CURLOPT_TIMEOUT, ($timeout+1));
			curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);		
			if ($post!==false){
				curl_setopt($curl, CURLOPT_POST, true);
				curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
			}
			if ($cookiejar!==false){
				curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiejar);
			}
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);	
			$page= curl_exec($curl); 
			$error= curl_error($curl);
			curl_close($curl);
			if (strlen($error)>0){$page= $error.' / '.$page;}
		}
	}
	return $page;
}
?>

  • 0

New around here? Click here to register and start participating in under a minute?

Or do a quick search and you may find the answer you're looking for.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download