...

View Full Version : Have web page load and scroll to specific div



inchecksolution
07-22-2011, 06:08 PM
Hi guys,

Hopefully I have posted this in the correct section.

My website has lists of information. Each piece of data is in it's own div, and each has a unique id. ex:



<div id="2220">
</div>
<div id="2221">
</div>
etc.....


I want to be able to create external links like: http://www.mysite.com?id=2221

And when someone clicks on the site the page will automatically scroll down to the div that has the id 2221.

Is this possible?

Rowsdower!
07-22-2011, 06:14 PM
Using the URL structure you are showing us you could do this with javascript (which I would argue is a bad thing to do).

To do this the conventional way you should be using a URL structure like this:

http://www.mysite.com/#2221

inchecksolution
07-22-2011, 06:18 PM
Amazing. Thank you Rowsdower!!!!

Rowsdower!
07-22-2011, 06:19 PM
Happy to help. And thank you for marking the thread as resolved!! :thumbsup:

Too few people bother to do that...

inchecksolution
07-22-2011, 07:00 PM
lol it has now been moved to unresolved haha. It has brought up another question.

It is working and scrolling to the proper div using http://www.mysite.com/#2221 BUT is there a way I can assign the 2221 (or whatever the number happens to be) to a php variable? I want to be able to use that number in a mysql query. I don't know any other method except the $_REQUEST method but I assume that it won't work in this case.

THANKS!

Rowsdower!
07-23-2011, 03:45 PM
The server never sees the part after the hash symbol so you can't directly access anything from the hash portion. You can use a standard $_GET type of URL as the base like in your initial post and then combine it with the hash thingy like so:

http://www.mysite.com?id=2221#2221

That should get you the best of both worlds.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum