Update window hash (at url) - javascript

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.

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.

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/

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.

Related

How to replace part of any matching URL in Firefox before loading

There are several variations of the question how to replace part of the URL with JavaScript?
but the solutions work for a current url, so first you load a page and then click on the bookmark that stores the javascript code to go to the edited location.
Is there a way to make this a default in Firefox? That is, if I click on a link and that link contains a specific chunk /epdf/ I want that chunk replaced with /pdf/ before loading the page in the first place.
I have created a bookmark that contains the script
javascript:var url = window.location.toString(); window.location = url.replace(/epdf/, 'pdf');
but that has the limitation I described above. Is there a way to perhaps load that script as a plugin on Firefox?
(I am way out of my depth here, not even sure what search terms to use for this)

Using Javascript to alter every url on page

I am not sure if there is a name for what I want to do, but I would like to use javascript to alter every url on a webpage on my website. So if someone posts a url such as:
visit http://www.blah.com
when viewing this page, I want javascript to change it to:
visit http://www.mysite.com/count.php?out=http://www.blah.com
I know about the two urls in one I will handle that part, I just used this as an example of placing the same text in front of every url in the page. Kinda like how thewaybackmachine does it. Thanks.
Using jQuery:
$("a").attr('href', "http://test.com?blah=" + $(this).attr('href'));
This code searches for all a tags on the page, and replaces the contents of the href attribute with http://test.com?blah= followed by the original contents of the href attribute.
To try it out, You can copy it, hit 'f12', paste it into the console, hit enter, and see it's effects on this page right here! (also breaking all your links...)
Here's a way, without the need for JQuery. (loading and parsing a pretty big library in order to just do one simple thing may be a bit much..)
[].slice.call(document.querySelectorAll('a')).map(function(a){ a.href = 'http://somesite.com/count.php?' + a.href })

Link to different parts/tabs of a page

Have a look at http://keithloutit.com/#news
I have a similar site, with all the content on the same page.
I would like to know how to fetch the correct part of the url. In this case it would be "news". I suppose its some regexp used on document.location.href? Hope you get the idea, otherwise ill try to elaborate. Thanks
The javascript on the page has to get the hash from the URL:
window.location.hash
and then activate the currect tab.
Note: if you're merely linking to a part of your page that is not part of a tabular system, you don't need javascript. The page will automatically scroll down to the element with that ID.

Mobile changePage Url adding root directories

Everytime I click to load the video in the new page, the url parameters are getting the base then repeating themselves.
So it goes from /screen/mobile/videos.asp to /screen/mobile/videos.asp#/screen/mobile/videoplayers.asp?id=b7c5z654vz_ak0
I've played around with it so much, but it seems no matter what I do it always resorts to the above, even when using absolute urls.
$(".videolink").unbind("click").click(function(e) {
e.preventDefault();
var data = $(this).attr("href");
$.mobile.changePage("videoplayer.asp?id="+data);
console.log('changing to videplayer.asp');
});
Need to find a work around to it as the urls are not friendly at all.
Did you try specifying an absolute URL? For example,
$.mobile.changePage("/videoplayer.asp?id="+data);
Whenever JQM loads pages with AJAX it will do that - since it only appends the new div to the current DOM (it's not actually reloading the full page).
If you want to disable this AJAX loading (and actually load a fresh page into the browser), you need to look into the documentation for example the AjaxEnabled property for setting this globally.
Hope this helps...

Search Engine Index Javascript Tabs

I have a website that is 1 html file and uses javascript to hide tabbed pages.
The url gets rewritten with a # for the different pages to make them bookmark-able.
Is there a way to make the different pages show in search engine results? It would be good to have them show up as different pages there.
I have read the below doc, but I think that is just for dynamically generated ajax content, right?
http://code.google.com/web/ajaxcrawling/docs/getting-started.html
I read the page mentioned by you. That is for Ajax site. In your case it is not Ajax.
Another point as Jeff B has mentioned is that the chance is high that Google will index all content for each trick you use. In that case it would be bad as Google will get duplicate content. It will be not very bad as all content are from your site only.
Search Engine questions like this are very tricky and difficult to answer as no one know the exact functioning of Search Engine.
In my thinking you either recreate your pages as Ajax and follow the points mentioned in article you got. Or
Use a link for each tag with param. like page1.php?cat1, page1.php?cat2, etc.
and that only load content related to specific tag at a time.
The second solution is no different than implementing different page for each tab, but it can be easier to update in your case! and also all content are still accessible by both person and search engine at a place. Slowly search engine will index your each page with parameter. Remember, It is generally said that Google does not index pages with parameter but it is not true. Google does not index page with variable or id kind of parameter only. They index each page with popular parameters if page content changes.
Still your question is tricky and my suggestion is what comes to me after thinking much about it.
The problem seems to be that even if the different pages were indexed, they would all index the same content. This is because according to your explanation all of the content (including hidden) exists at load time.
If your tabs are links, you simply need to put the href in the link. Google should follow this link, while javascript-enabled browsers will execute your tab-switching code and not follow the link (if you coded it right).
However, the problem of all content being indexed for all pages still remains.
Modify your system like this:
Every link that changes the content of the current tab should have
as href attribute a subpage that contains the content of the tab
intended to appear -> this will be cached by Search Engines.
Those links should have binded JS actions that changes the content
of the current tab and also denies the redirecting that should have
been done by what's in the "href" attribute -> this will be shown to
the user

Categories