0

So I have this js code for an image gallery:

(this.settings.update_window_hash) {
        var thumb_link = this.images[this.current_index].thumb_link;
        if (thumb_link.attr("id")) {
          window.location.hash = "#image-"+ thumb_link.attr("id"); //#url
        } else {
          window.location.hash = "#image-"+ this.current_index;
        };
};

So as you've probably assumed this appends $image-(int) to the url. So if I have a gallery with multiple images if the thir image is selected the url will look like this: mysite.com/gallery.html#image-3

All good. But I dont really like this to be appended to the end of the url. So is there any problem if I remove this part of the script entirely? So regardless the number of image currently selected the url will look like this: mysite.com/gallery.html

I've tested it and it works okay. But I'm not very experienced with javascript and I want to make sure I'm not making a mistake. So IS IT OKAY IF I REMOVE THIS SCRIPT ENTIRELY? WILL IT CAUSE ANY PROBLEMS?

HUGE THANKS.

inrob
  • 4,969
  • 11
  • 38
  • 51

3 Answers3

1

Hashes at the end of the URL are optional and not required so YES, you can remove that script if you want (I'm not sure what problem you're trying to solve by removing it). In general, you get more useful answers if you tell us what problem you're trying to solve rather than what solution you're trying to use.

Hashes are used when you want the URL of the page to direct the viewer to some subcontent on that page. If you remove them, your page will still work just fine, but the URL of the page will not reflect which image is displaying. So, if the viewer saves that URL and comes back to it or links to it or anything that keeps a reference to the URL, it will go to the generic version of the page, not the onethat shows a specific image. Whether that is OK is totally up to you and how your page works.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Hello. Well that responds to my question. I dont really want them in the url as they are useless in my case. The js is part of a gallery/image slider and the url only shows the current selected image so I'm not using it in a webpage or elsewhere (where it can help navigate to the desired section of the page).Thank you for your answer. – inrob Jul 11 '12 at 20:41
0

Just use:

location.replace(location.href + "#myhash");

The location.replace method overwrites the current step in browser history. For an example of this in action see http://prettydiff.com/slideshow/

austincheney
  • 1,189
  • 9
  • 11
  • Yes I'm aware of that. But as I said, since i'm a newbie in js just wanted to make sure I wasnt doing anything wrong. – inrob Jul 11 '12 at 20:43
0

The stuff after the octothorpe normally represents a "name" or "id" from the web page. You can have an anchor tag (<a name='thevalue'>) and the browser will interpret the text after the octothorpe (http://example.com#thevalue) by scrolling to the associated section on the page.

Unless the page has special JavaScript to behave differently. In your case, it depends upon the full functionality of the web page you're writing. If you have smoke tests/unit test/use case tests/other QE tests, you should execute those to ensure that your changes don't break anything.

See http://www.w3schools.com/html/html_links.asp for more description of the standard usage.

atk
  • 9,244
  • 3
  • 32
  • 32