PDA

View Full Version : using url like a php query string



Agrajag
05-24-2006, 04:58 AM
hi

i've written a script which switches between divs of a certain class on the page when appropriate links are clicked.

for the sake of accessibility, the script first hides all the divs, then shows one of them. this way, if a visitor does not have javascript enabled, the divs will show normally down the page. i've also included named links for anyone without javascript.

what i want to know is, can i use the #linkname on the end of a url to automaticall switch to the right div when the page loads?

that way, a bookmarked page would go straight to the right section

thanks.

djr33
05-24-2006, 05:12 AM
Generally with php, it's ?var=value&etc=etc, not #blah

though that can work too. the # is more for anchors (like... snapping to the bottom of the page). Not sure if there're more uses, though.

I don't know the script off hand, sorry.

However, "Is it possible?"... yeah, definitely.

If you know php alredy, you could have it export some of the javascript... a value of a variable...

<script>
var = "<?php echo $_GET['var']; ?>";
...
</script>

That would be the easy way for php to do it for you.

However, you may want to use javascript alone... can't be too hard... I just don't have the script at the moment.

mwinter
05-24-2006, 10:30 AM
for the sake of accessibility, the script first hides all the divs, then shows one of them. this way, if a visitor does not have javascript enabled, the divs will show normally down the page. i've also included named links for anyone without javascript.Sounds good so far.


what i want to know is, can i use the #linkname on the end of a url to automaticall switch to the right div when the page loads?You certainly could parse out the fragment identifier and, if present, use that rather than the default.



function getFragment(uri) {
var i = uri.indexOf('#');

if (i != -1) return uri.substring(i + 1);
return null;
}
So, for example, you might write something like:



if (document.getElementById) {
var id, element;

/* If the URI contained a fragment identifier... */
if (!(id = getFragment(document.URI))
/* ...and that fragment identified an element within the
* document, use it to reveal that element.
*/
|| !(element = document.getElementById(id))) {
/* Otherwise, assume the default. */
element = document.getElementById('defaultId');
}
/* If an element was obtained and it has a style object... */
if (element && element.style)
/* ...show that element. */
element.style.display = '';
}
Hope that helps.



the # is more for anchors (like... snapping to the bottom of the page). Not sure if there're more uses, though.The purpose of fragment identifiers in URIs is defined by the URI scheme and the context of its use. In HTML, it provides a means for linking to destination anchors (<a name="..."> or <... id="...">). Its use varies elsewhere.

Mike

shachi
05-24-2006, 12:42 PM
try reading this: http://www.javascriptkit.com/javatutors/send1.shtml

djr33
05-24-2006, 05:31 PM
Yeah, thanks for clearing that up, Mike.

Agrajag
05-25-2006, 06:33 AM
hi
sorry i couldn't explain in more detail. i was at school and it was nearly home time, so i didn't have time to upload an example or think about what i was writing :D.

so here's an example for you to look at:
http://zz92.awardspace.com/div%20switcher.html

basically what i want it to do is, instead of hiding all divs then showing one, when a link is clicked check the url for #n and then show the corresponding div.

i hope that explains everything.

also, can anyone tell me why the show_all() function doesn't work? it's meant to show every div like you would see normally without javascript, but in firefox it shows them all for a second then changes back. in ie i does something really weird.

thanks for all your help.

agrajag

djr33
05-25-2006, 07:07 AM
If it shows all and switches back, there's something else, perhaps "hide_all" or something, being activated by... something. Maybe just by default.
Try to see what might be being activated.
I'd help, but not sure myself. If someone else doesn't figure it out, I'll take a closer look and try to see it, but I'm sure Mike or someone will know :)

Agrajag
05-25-2006, 12:29 PM
i've had another look at it, and i'm fairly sure that somehow, clicking the show all link triggers the body's onLoad event. no idea why though, or how to stop or it from happening, can someone please take a look at it?

thanks

shachi
05-27-2006, 06:15 PM
I hope this fulfills your needs: http://www.anaesthetist.com/mnm/javascript/

The example is at the bottom of the page (Enter your name here) and to skip the first page and go to directly the tutorial change http://www.anaesthetist.com/mnm/javascript/ to
http://www.anaesthetist.com/mnm/javascript/part2j.htm#<yourname>

This captures the text or anything else from a URL after the # character with javascript and displays the result.