Hi there, regards to everyone. I really don't know if this is the right place to post this question because I can't find out if the problem is the script or something with my markup. But anyways, I need help. I live in Mexico City and am building a site which uses the funtion created by Patrick Griffith, author of the HTML Dog. If there's need I paste the function at the end of this message. I've previously worked with the function and worked fine. But this time I have a trouble that I can't find out if it's something related to the browser or what is it. Here's the thing: I have a scrollable div set to overflow x: hidden and overflow y:scroll, there I point to three anchors that should display the related three divs which display is hidden except when they are targeted. Of course the page works fine in Firefox, Safari, Opera, and other common browsers, but the point is that it should work as well in IE with the script to cheat it. I started building the page and noticed there was a problem when the div's were displayed, sometimes it didn't work. I seriously ignore if there's some trouble with the markup, I tried checking the tags in the lists that and unordered lists where the anchor is, and nope. But, suddenly, it worked! Just as expected, the divs are displayed when the link is clicked. I also use the same trick to display the rest of 11 divs where I have photos of the products on sale. But, here goes the question: Is there a trouble with a scrollable div, with positioning (the containing div is relatively positioned, which I figure should be fine) or with using anchors in a list, etc. As stupid as may sound, I ask this question because the page loads fine, except when I hit the back button and forward button of the browser (am using IE 8, haven't tried with IE7 nor IE6). The thing is when I hit the back button the page loads with an error, ahd then the anchors are useless. Please help! I don't know enough Javascript to understand if there's a trouble that has to do with the specific markup, the style rules or the script. How does it relate to the way the back button works? If there's need I'll post the entire markup, and finally I post the function. Any hint will be greatly appreciated.
sfTarget = function() {
var sfEls=document.getElementsByTagName("H2");
var aEls = document.getElementsByTagName("A");
document.lastTarget = null;
for (var i=0; i<sfEls.length; i++) {
if (sfEls[i].id) {
if (location.hash==("#" + sfEls[i].id)) {
sfEls[i].className+=" " + cls;
document.lastTarget=sfEls[i];
}
for (var j=0; j<aEls.length; j++) {
if (aEls[j].hash==("#" + sfEls[i].id)) aEls[j].targetEl = sfEls[i]; aEls[j].onclick = function() {
if (document.lastTarget) document.lastTarget.className = document.lastTarget.className.replace(new RegExp(" sftarget\\b"), "");
if (this.targetEl) this.targetEl.className+=" sftarget"; document.lastTarget=this.targetEl;
return true;
}
}
}
}
}
if (window.attachEvent) window.attachEvent("onload", sfTarget);
That's the function that works fine, thanks to Patrick Griffith, and although I can gather that it goes through the tags I need (of course I work with DIV and not Hn) and renames the class applied to it by a regular expression, I can't figure what's going wrong.