You are here: DEVPPL Forum Programming JavaScript Forum
NOTIFICATIONS
54.073
MEMBERS
15.680
TOPICS
62.249
POSTS
  562
FLASH GAMES
7.740
TUTORIALS
 

Login

E-mail:
Password:

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

0

Loading

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

Postby Vid Warren » Sun Feb 20, 2011 7: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
 
Reputation: 0
Posts: 16
Joined: Thu Feb 14, 2008 5:27 pm
Location: Bristol
Highscores: 0
Arcade winning challenges: 0

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

Sponsored results

Login to get rid of ads

 

0

Loading

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

Postby Vid Warren » Mon Feb 21, 2011 3: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
 
Reputation: 0
Posts: 16
Joined: Thu Feb 14, 2008 5:27 pm
Location: Bristol
Highscores: 0
Arcade winning challenges: 0
^ Back to Top