views:

36

answers:

2

Hello there,

I have a video gallery that has a menu on the left and loads the content into a div on the right hand side. The menu is generated from php video posts so we need a general script that will effect everything.

-- The problem --

The links will load the URL of the video as an anchor on the current URL -

eg.

http://www.divethegap.com/update/community/videos/#http://www.divethegap.com/update/2010/10/test-video-2/

So all I need is a script that will get the hash tag and load the content into a div. So far I have failed miserably trying to do that. I imagine it is something along the lines of document.location.hash but don't know where to go from there.

Help much appreciated.

Thanks

A: 

Note: this answer users jQuery because 1.4.2 is included and being used extensively in the page already.

You can attach a click handler to the anchors, for example:

$("#nav a").click(function() {
  $("#content").load(this.hash.substring(1));
});

You can test it out against your markup here, not it won't actually load in the demo since it's on a separate domain, but will work fine on the same domain.

Nick Craver
Tried it out. Seams to have done nothing. Tried altering it and still nothing. Its all online if you want to take a look. The script is in update/z-js/inloader.js.... Even if it was a script that was called that would be fine as it could be called on body load as well to direct an external link to the correct video on the page.
Robin Knight
@Robin - If you're including it in a new file, be sure to wrap it in a `document.ready` handler, put a `$(function() { });` around what you have now.
Nick Craver
Marvellous that did it. But brought up another issue. If you try it now it is working but the 'sagscroller' on the left is not working with the links properly. It works with the first incanation but not the second as you can see. I assume this is something to do with multiple ID's being generated by the scroller. Other problem is that we need the hash tag to load the correct video on page load if the link to the page was for example http://www.divethegap.com/update/community/videos/#http://www.divethegap.com/update/2010/10/test-video/
Robin Knight
@Robin - IDs have to be unique...you must fix that first before all other issues related to IDs, scrolling (among many other things) *depends* on them being unique.
Nick Craver
I think we will have to dump the sagscroller for that. The more important issue is getting it to load the correct video on page load so if someone has a link to http://www.divethegap.com/update/community/videos/#http://www.divethegap.com/update/2010/10/test-video-2/ it will load the page and load the correct video. Thanks
Robin Knight
@Robin - You can use an onload function for that, beside the above code: `if(location.hash) $("#content").load(location.hash.substring(1));` As an aside, if something's generating invalid HTML, get rid of it, it'll only cause problems...just like it is now :)
Nick Craver
I thought you'd say that and I agree. Just to clarify with the onload should it be like this <body onload="if(location.hash) $("#content").load(location.hash.substring(1));" or should it be <body onload="inload()" and make a js function for inload of if(location.hash) $("#content").load(location.hash.substring(1)); Many Thanks.
Robin Knight
@Robin - it should run in the same `$(function() { });` block as the above code.
Nick Craver
marvellous. Many Thanks
Robin Knight
@Robin - welcome :)
Nick Craver
Sorry Nick one more question. As it is now the page loads a facebook comments box but that does not seam to load through jquery with the rest of it. It will load on the initial load though. Any ideas.
Robin Knight
@Robin - I'm seeing `fb:comments` in the HTML directly, looks like these tags aren't being converted to whatever the equivalent markup is server-side.
Nick Craver
Hello Nick, its me again. If you take a look at the page now you will see all is working now as expected. However we are now having some browser problems. Safari is working properly. Firefox 3.6 is working but 3.1 will not load Test Video if Test Video 2 is loaded. IE is not doing anything at all. I don't know if this is the jquery function or differences in the rendering of DIV relative positioning. Any Ideas. Many Thanks.
Robin Knight
A: 

You can try this -

$('a').click(function(){

  $('#content').load(document.location.hash.replace(/#/,''));

});

This will load the content after hash part from the current url.

Alpesh
Surely that would make it happen with every link on the page
Robin Knight