It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

Syntax help? Getting a div to scroll down using the URL

Syntax help? Getting a div to scroll down using the URL

Postby Vid Warren on Sun Feb 20, 2011 6:02 am

I have all of the pieces of the puzzle working separately:

This code performs the function 'move_up()' when I add '?biog=2' on the end of the URL:

Code: Select all
(function () {
var $_GET = (function () {
  var a, i, o = (window.location.search.substring(1)).split('&');
  for (i in o) {
    a = o[i].split('=');
    o[a[0]] = a[1];
  }
  return o;
})();

if($_GET['biog'] == 2) {
  move_up();
}
else {
  alert('no luck');
}
})();


This code scrolls the div 'scroll_clipper' down by 50px:

Code: Select all
  function move_up() {
    scroll_clipper.scrollTop = 50;
}


The problem is, they don't work together. 'move_up()' scrolls the div down perfectly when triggered by a link:

Code: Select all
  <a href='javascript:move_up()'>Move UP</a>


And, to test the first code, I've tried changing 'move_up()' to:

Code: Select all
  function move_up() {
document.write('Hello);
}


Which (with the first code) writes hello when I load the page as http://www.example.com/page.html?biog=2

Full code here (to prove it works):

Code: Select all


  function move_up() {
    document.write('Hello');

  }

(function () {
var $_GET = (function () {
  var a, i, o = (window.location.search.substring(1)).split('&');
  for (i in o) {
    a = o[i].split('=');
    o[a[0]] = a[1];
  }
  return o;
})();
// Now the GET querystring values are accessible via $_GET['key']
// for example, given querystring ?name=Fotiman&var=1&foo=bar
// if ($_GET['var'] == 1) {
//   dosomething
// }
if($_GET['biog'] == 2) {
  move_up();
}
else {
  alert('no luck');
}
})();



Why do they work in all of those combinations but not the one I need?

I've got the basics of JavaScript but I'm still new to it. Any help is greatly appreciated.
Vid Warren
 
Posts: 16
Joined: Thu Feb 14, 2008 4:27 pm
Location: Bristol

Re: Syntax help? Getting a div to scroll down using the URL

Postby Vid Warren on Mon Feb 21, 2011 2:22 am

BAM! Fixed it:

Code: Select all
<div id="scrollable" style="overflow: hidden; height: 600px; border: 1px solid #000">


<script type="text/javascript">
(function () {
var $_GET = (function () {
  var a, i, o = (window.location.search.substring(1)).split('&');
  for (i in o) {
    a = o[i].split('=');
    o[a[0]] = a[1];
  }
  return o;
})();
// Now the GET querystring values are accessible via $_GET['key']
// for example, given querystring ?name=Fotiman&var=1&foo=bar
// if ($_GET['var'] == 1) {
//   dosomething
// }
if($_GET['biog'] == 1) {
window.onload = function () {
    document.getElementById('scrollable').scrollTop = 700;
  }
}
else if($_GET['press'] == 1) {
window.onload = function () {
    document.getElementById('scrollable').scrollTop = 1400;
  }
}
})();
</script>

  <p>
  </p>

etc.

</div>



Two things, the div needed to have loaded first (window.onload) and the script needed to be put inside the div it affects, not sure why that is.

This is going to open up a lot. :)
Vid Warren
 
Posts: 16
Joined: Thu Feb 14, 2008 4:27 pm
Location: Bristol


Who is online

Users browsing this forum: No registered users and 6 guests