| You are here: DEVPPL ‹ Forum ‹ Programming ‹ JavaScript Forum |
NOTIFICATIONS
|
|
|||||||||||||||
Login |
Syntax help? Getting a div to scroll down using the URL
2 posts
• Page 1 of 1
0
Syntax help? Getting a div to scroll down using the URL
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:
This code scrolls the div 'scroll_clipper' down by 50px:
The problem is, they don't work together. 'move_up()' scrolls the div down perfectly when triggered by a link:
And, to test the first code, I've tried changing 'move_up()' to:
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):
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.
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
0
Re: Syntax help? Getting a div to scroll down using the URL
BAM! Fixed it:
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.
- 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
|
|