bookmarklet to swap URL with URL/directory - javascript

I'm trying to create a bookmarklet (something I've never done) that will read the URL and check if it ends with "/directory". If it does, I want to remove "/directory". If it doesn't, I want to add it.
I'm using the following to successfully append the directory to the URL, but I don't know how to check if it's already there or delete it if it is.
javascript: window.location = window.location.protocol + '//' + window.location.hostname + window.location.pathname + '/directory';

This will do it:
var URL = window.location.href; window.location.href = /\/directory$/.test(URL) ? URL.replace(/\/directory$/, '') : URL + '/directory';
Checks if the url ends with /directory if does, removes it, else adds it.

Related

javascript window.location not working

I am new to javascript, and stuck on this problem. I am trying to have the url query string change based on which checkbox checked. But I seem to have some issue with window.location, Some of the JavaScript is below
var urltest= window.location.pathname;
var urltest2 = window.location.host;
var currentURL = urltest2 + urltest;
url = currentURL + "?" + arr_opts.join('&');
document.getElementById('myurl').innerHTML = url;
//window.location.href = url;
window.location = url;
The window.location does not work here, but when I change var currentURL to
var currentURLL = window.location.href;
it is work, but not with
var urltest= window.location.pathname;
var urltest2 = window.location.host;
var currentURL = urltest2 + urltest;
I need window.location to point page to the currentURL above.
Any help would be appreciated.
You're not providing a protocol, so the location.href change is being treated as "go to this relative path from the current location", i.e. on this page
window.location = window.location.host + window.location.pathname;
// takes us to
// http://stackoverflow.com/questions/35254564/javascript-window-location-not-working/stackoverflow.com/questions/35254564/javascript-window-location-not-working/35254601
Do one of the following
Provide a protocol so it knows it is an absolute URI,
window.location = window.location.prototcol + '//' + window.location.host + window.location.pathname + '?foo=bar';
Tell it to re-use the current protocol but work as an absolute URI
window.location = '//' + window.location.host + window.location.pathname + '?foo=bar';
Provide an origin (instead of host)
window.location = window.location.origin + window.location.pathname + '?foo=bar';
Tell it to re-use the same origin but work as an absolute path
window.location = window.location.pathname + '?foo=bar';
Just update the query
window.location = '?foo=bar';
Always choose the most simple option to make your life easier if you ever need to debug, i.e. if you can assume you will always want the same protocol, host and path, just update the query.
Useful knowledge
Starting a URL with..
// means same protocol
/ means same origin
? means same path
# means same query (will not re-load)
The pathname is going to be something like /questions/35254564/javascript-window-location-not-working, and the host is something like stackoverflow.com. If you put those together with code like yours, you get
stackoverflow.com/questions/35254564/javascript-window-location-not-working
That's clearly not correct; the host looks like part of the pathname. You can use protocol and port if you really feel the need to reconstruct the URL from its constituent parts, but using the href seems simpler if all you want to do is add a query string.

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.

Trim URL using javascript

I'm making an ebay template for myself, and I want to use a name anchor to jump to different section on the page template. But Ebay adds something to the URL therefore breaking the name anchor.
Since this seems to be Firefox related only, someone suggested that I need to strip "&bv=mozilla" from the URL then it would work. If there are any javascript experts out here that can help me out, I would highly appreciate it.
var documentUrl = location.href;
var newUrl = documentUrl.replace("&bv=mozilla","");
Try this:
var url = location.protocol + '//' + location.host + location.pathname;
or this:
var url = location.protocol + '//' + location.host + ':' + location.port + location.pathname;
if it is on the same page.
Otherwise, you could try this:
var url = 'insert URL here';
var new_url = url.substring(0, url.indexOf('?'));
See substring, indexOf and window.location.

Current URL without parameters, hash, http(s)://

I'm looking for a neat way of getting the URL of the current document in Javascript.
The URL should be clean of parameters (?parameter1=bla&parameter2=bla)
The URL should be clean of hash tags (#jumppoint)
http/https should be removed/consolidated into http
I know i can get the current URL with location.href and then use some regular expressions to clean it up but maybe there is a nicer/cleaner solution for getting rid of the junk?
There are many other parameters than the href in window.location. See full reference here: https://developer.mozilla.org/en/DOM/window.location
What you are looking for as a starter might be the window.location.hostname:
"the host name (without the port number or square
brackets)."
From the example URL http://[www.example.com]:80/search?q=devmo#test the hostname will be www.example.com.
If you also want to include the path and force a http:// protocol, try:
'http://' + window.location.hostname + window.location.pathname;
As a side note, a nifty trick to get the same parameters from another URL than the window.location is to create an empty anchor:
var a = document.createElement('a');
a.href = 'http://www.example.com:80/search?q=devmo#test';
console.log('http://' + a.hostname + a.pathname);
None of the given answers address the fact that the protocol can be http or https as in the OPs title. To accommodate this I suggest:
document.location.protocol +"//"+ document.location.hostname + document.location.pathname
The Location object got what you need
window.location.hostname + window.location.pathname
You have document.location object, so:
var oLoc = document.location,
sUrl = oLoc.protocol + oLoc.hostname;
// or "http://" + oLoc.hostname
You can use these replacement functions to remove the hash and search arguments and normalize https to http:
url = url.replace(/#[^#]*$/, "").replace(/\?[^\?]*$/, "").replace(/^https:/, "http:");
Or, if all you really want is the domain and path, you can just use this:
window.location.hostname + window.location.pathname
Please try this snippet:
if (!window.location.origin){
// For IE
window.location.origin = window.location.protocol + "//" + (window.location.port ? ':' + window.location.port : '');
}
url = window.location.origin + window.location.pathname;
document.write('Origin url: ' + url);
This page indicates that you could probably use window.location.host to get the part you're actually interested in. I haven't tested it, though.
Try:
window.location.hostname;

Create and go to url with Javascript

I want to be able to produce a URL based on certain properties and then go to the new URL in javascript.
Here is what I have so far:
triggerNumber = document.findcontrol(txtTrigNo).text;
hostAddress= top.location.host.toString();
url = "http://" + hostAddress "/" + triggerNumber
How do I navigate to the new URL?
Simply try:
window.location = url;
But before trying to do that, you have to make sure the page at the address "http://" + hostAddress "/" + triggerNumber exists. For example by putting valid triggerNumbers in an array and check if it exists or not. So:
//Not sure if at the end it should be .text or .value or .value()
triggerNumber = document.findcontrol(txtTrigNo).text;
var validTriggers = [123, 456, 789];
if (validTriggers.indexOf(parseInt(triggerNumber)) == -1) {
alert("Invalid trigger number");
} else {
hostAddress= top.location.host.toString();
url = "http://" + hostAddress "/" + triggerNumber;
}
Finally, if the destination is a server-side page (php, asp, etc), the address usually looks like this:
"http://" + hostAddress "/trigger.php?id=" + triggerNumber;
but you'd better use forms for this.
Edit: As Cerbrus suggested, validating the values with javascript is a good way to tell the user about his errors before navigating away from the page. But to make sure the correct data is sent to server, it is important to do the validation in the server-side code, too.
In this example, in case of an invalid trigger number the user may finally see a 404 error; but with sensitive information worse things can happen.
What you need is:
document.location.href = url;
After you have the URL in the url variable.
To get value of input element have:
var triggerNumber = document.getElementById("txtTrigNo").value;
This will get the hostname and port of the server, and concatenate the value of the element onto the end, and then go to the resulting URL.
var triggerNumber = document.getElementById("txtTrigNo").value();
var url = "http://"+window.location.host+"/"+triggerNumber;
window.location = url;

Categories