Browser shortcut - add url - javascript

I needed to help with a feature I have never seen before. I do not even know if it exists, but you may have met.
I'm referring to the shortcuts in the browser and I would like to add another shortcut to "add 'this' to the end of the url".
Example:
I am on www.example.com and click on the link in the shortcut bar to redirect me to www.example.com/redir1. If I'm on www.example2.com and click on the same link, it redirects me to www.example2.com/redir1.
I manage a number of sites and I have to click on "Settings/blah blah/page/" takes a few minutes. By linking, I would like to get straight to the "page" so I do not have to click and load pages before this one so many times.

You may use a Bookmarklet.
Bookmarklets are (small) chunks of JavaScript, that will be executed when clicking the bookmarked link. I use them for quick navigation in ticket systems.
Maybe this example solves your problem.
Of course, you have to condense your JavaScript to just one line, so it fits into the address line of the bookmark.
javascript:(function(){open(window.location.protocol + "//" + window.location.hostname + "/redir1");})();
You can even open a JavaScript prompt for retrieving some kind of user input. The next example asks the user where he wants to go and modifies the link respectively.
javascript:(function(){var relPath=prompt('Where do you want to go?'); open(window.location.protocol + "//" + window.location.hostname + "/" + relPath);})();
I tested this one in the current versions of Firefox and Chrome. Just add a new bookmark and use the JavaScript Code instead of any URL.
Additional examples, as requested in comments.
For the sake of readability, I present the second one in multiple lines, please remove the line breaks before trying to use it as a bookmarklet.
The first example navigates from
protocol://sub.domain.tld/any/possible/path/somewhere.xyz to
protocol://sub.domain.tld/web1/site/site.xml.
javascript:(function(){open(window.location.protocol + "//" + window.location.hostname + "/web1/site/site.xml");})();
The second example navigates from
protocol://sub.domain.tld/keep/this/any/site.xml to
protocol://sub.domain.tld/keep/this/another/resource.
If window.location does not contain a long enough path name, the navigation will not work, because the script will just add "undefined" in the target URL.
javascript:(function(){
var pathNameAsArray = window.location.pathname.split('/');
var pathToKeep = "/" + pathNameAsArray[1] + "/" + pathNameAsArray[2];
open(window.location.protocol + "//" + window.location.hostname + pathToKeep + "/another/resource");})();

Related

Redirect to different URL when current URL contains some string

I have a requirement wherein I need to redirect my page to a different URL when my current URL contains some string.
For instance,
If my current URL contains www.testdomain.com or www.testdomain.com/web/region then it should redirect to www.testdomain.com/group/region. I tried the below code but it returns "The requested resource could not be found -- https://www.testdomain.com/web/region/testdomain.com/group/region".
$(document).ready(function() {
if (window.location.href.indexOf("web/region") > -1) {
window.location.href=window.location.hostname+'/group/region';
}
})
It is adding the URL twice here. But when I pass the direct URL window. location.href="www.testdomain.com/group/region" it is working.
Can someone guide me on how do I force redirect my page if the URL contains www.testdomain.com or www.testdomain.com/web/region?
Thanks
Start with // so that the browser knows it's not a relative URI:
window.location.href = '//' + window.location.hostname+'/group/region';
You can also prepend the protocol:
window.location.href = window.location.protocol + '//' + window.location.hostname+'/group/region';
It's adding the URL twice because browsers interpret /group/region as a relative path, automatically prepending the current domain UNLESS otherwise specified. (Maybe others can explain why window.location.hostname doesn't immediately return, thus preventing the browser from assuming a relative path?)
Example: If you explicitly set a domain, the browser will redirect to it as expected.
window.location.href='http://www.google.com/'
Otherwise, if you take away "www."
window.location.href='google.com/'
Your browser will redirect to "www.testdomain.com/google.com", appending the string.
The fix is simple.
Just delete window.location.hostname+ and it will only return the URL once.
Or... for a better user experience, I would suggest using window.location.replace() which DOES NOT save the current page in session history.(You don't want to go back, just to be redirected again!)
SOLUTION:
Replace your return block with this.
window.location.replace('/group/region')

How do I disable facebook's ?brand_redir and open new page without extra piece of URL

I'm trying to disable facebook's redirection to another country page by restructuring its URL.
An example would be - you're trying to open https://www.facebook.com/VisaID/ but if you're accessing the page from another country, you're going to be redirected to the closes page based on your geolocation (I guess). In my case, it's https://www.facebook.com/VisaCzechRepublic/?brand_redir=272936492828277 and I want to prevent this.
I tried to write a piece of code to solve this, however, when I try to reload the page with restructured URL it somehow adds extra window.location.hostname and window.location.pathname resulting in an absolute mess and breaking the page...
result = https://www.facebook.com/VisaCzechRepublic/www.facebook.com/272936492828277/?brand_redir=DISABLE
Code I used:
//Split URL to get FB ID
var brand = window.location.search;
brand = brand.split("=");
brandId = brand[1];
//Create new URL
//First facebook.com
//Second Page ID
//Third add ?brand_redir=DISABLE at the end
url = window.location.hostname + "/" + brandId + "/" + "?brand_redir=DISABLE";
//Open rearranged URL
window.open(url)
I expected Chrome to open a new URL exactly as is saved in the url being https://www.facebook.com/272936492828277/?brand_redir=DISABLE
but it somehow sneaks in extra https://www.facebook.com/VisaCzechRepublic/ and only after that ads the url I want.
Thanks a ton for any advice!

How to make a ssl forwarding script that runs once per page load

I have been having some trouble using a javascript script that automatically makes you get forwarded to use ssl. example,
http://www.example.com/link becomes https://www.example.com/link
But my issue is that is continuously loads the script, but I want it to stop when it is already loaded. It reloads continuously, making it very annoying and hard to click the links on the page.
Here is my current script
window.location = "https://" + window.location.hostname + window.location.pathname + window.location.search;
You need this:
if(window.location.protocol == 'http:') {
window.location = "https://" + window.location.hostname + window.location.pathname + window.location.search;
}
...or even better:
if(window.location.protocol == 'http:') {
window.location.replace(window.location.toString().replace(/^http:/, 'https:'));
}
Second variant is better because:
URL may be complex and will be handled properly
Because of using window.location.replace() instead of directly assigning a string to window.location, previous URL will be removed from history and when user clicks 'back' button, he will jump to original page, not to the one with "http:" protocol: http://www.w3schools.com/jsref/met_loc_replace.asp
But it's better to implement this on server side.

How can I modify this javascript to ignore any data within a query string?

Essentially, I have this website, the content of which changes depending on what the user inputs into the query string.
When the user enters mysite.com/?1 it loads content for 1 and /?2 for 2
My problem is that I have a Facebook like button within my page, and to make it work I have written this js code:
<script type="text/javascript">
var sUrl = window.location;
document.getElementById('fbcom').setAttribute('href', sUrl);
</script>
this gets the url and allows the user to like different content from what is technically one file.
My problem is that when a user likes for example /?1 on facebook, if someone where to click this link on their newsfeed and decide that they like it too, technically they will be liking the page /?1-with all the additional facebook code on the end of the url, so heading back to /?1 the like has not registered.
How can I modify the above code to ignore any facebook rubbish on the end of the url when they are directed from facebook?
Important: the ID /?1 can be anything from a 1 digit to a 4 digit number e.g /?1234
My current JS ability is very poor. Thanks
You can combine the properties of location you actually want to keep -- which seems to be protocol, host, and pathname:
var sLoc = window.location;
var sUrl = sLoc.protocol + '//' + sLoc.host + sLoc.pathname;
You can also just use the pathname as relative-from-root:
var sUrl = window.location.pathname;
you can do that with regex:
var sUrl = window.location.toString().replace(/^(.*?)(\?.*)?$/, '$1');

Create hyperlink based on current URL

I created a bilingual website using two databases:
www.martyregan.com/
www.martyregan.com/jp/
You can choose the language of the website using the 'Website Language' flags, but currently the links only bring you to the homepage. The paths/URLs on both sites are exactly the same, other than /jp/ directory on the Japanese site.
I'm looking for a way to alter the hyperlinks to go to the parallel page, based on the URL of the page the visitor is currently on. I figure it'd be quite simple being that the paths are identical, but not really sure where to start with my little knowledge of jquery.
This assumes your language is accessible as seen below.
"http://" is removed from the URL for convenience.
$(function(){
var lang = 'jp';
$('a').attr('href', function(x, url){
var split = url.replace(/(http:)?(\/\/)?/, '').split('/');
return split.shift() + '/' + lang + '/' + split.join('/');
});
});
You may be able to get away with using the <base> tag depending up on the browsers you need to support. You can just set that to whatever the base is by acquiring it from the server.
If you want to use jQuery to do it, it should be fairly simple if all of your hrefs are absolute:
$("a").attr('href', function (_, href) {
return $("base").attr('href') + href;
});
This assumes that you are using <base>. If you're not you can get the path from window.location.path, or even some other element on the page.
In case you are confused, the JavaScript is only required if <base> is not enough to work on its own

Categories