Return to Snippet

Revision: 59133
at August 19, 2012 20:06 by chrisguiblin


Initial Code
<script languate="javascript" type="text/javascript">
	
	//Get the values from the url variable id
	//(getUrlVars Code URL: http://snipplr.com/view/19838/get-url-parameters/)
	function getUrlVars()
	{
		var vars = {};
		var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value){
			vars[key] = value;});
		return vars;
	}
	
	//scroll to the specific div, assuming each div is named "div_(number)"
	function scrollToID() 
	{
		document.getElementById("div_" + id).scrollIntoView();
	}
	
	var id = getUrlVars()["id"];	
	window.onload=scrollToID(id);
	
	</script>

Initial URL

                                

Initial Description
This code assumes a page is populated with divs with ids div_1, div_2, div_3 etc etc etc...

Some quick php to create 200 divs numbered as above:

<?php>".
			 "	DIV ".$i.
			 "</div>";
	}
?>

To test the code, add an id to the url so its something like this (assuming urlVars.php is the name of the page):

urlVars.php?id=80

Play around with the id number to test.

Initial Title
Scroll to specific div using a variable from the URL on page load

Initial Tags

                                

Initial Language
JavaScript