Remove the # character from url - javascript

I have a js-slider and it's url is changed every time, when changes pictures.
I want to remove /#/ from url.
document.location.href = String( document.location.href ).replace( "#/", "");
It's work, but plugin jquery.address.js doesn't view page and redirected to 403.
How can I remove this symbol and the slider would work.
site: http://taron-julhakyan.ru
Thankes!!!

You can use regexp in javascript
document.location.href = document.location.href.replace(**/(#)[0-9A-Za-z-]+/ig, "#/"**);
Here the /ig suffix says that the regex is case insensitive and and global stating.
For more information refer here

Related

The url containing the # is truncated when tested with window.location.href in IE

If the url contains a #, when I run console.log (window.location.href), you may get the string after the # and sometimes not.
I have tried console.log(decodeURIComponent(window.location.href)) too.
But this method also produced the same result.
How do you always get the character after the # in IE?
No problem with chrome.. 😭
The url I tested looked like this:
http://aaa/bbb.html#param
After getting the location.href, you could use the indexOf() method to check whether the url contains #, then using the substring() method to get the url without #.
Sample code as below:
var href = window.location.href;
if(href.indexOf("#")>-1){
href = href.substring(0, href.indexOf("#"));
}
console.log(href); //output: http://aaa/bbb.html

How to replace the forward slash in a javascript bookmarklet

I am not a programmer, but know a little here and there. This is a bookmarklet I have in my browser. It is supposed to take the url of the page I am on, and when clicked, takes me to another site (example.com), and pass this first site into the url of the second site (e.g. sitechecker.com).
Problem is, the trailing slash on the example.com/ prevents sitechecker from working, so i need to get rid of the trailing slash somehow when its passed to the other site.
E.g.
No Good
http://www.example.com/
Good
http://www.example.com
Bookmarklet code:
javascript:(function(){ var url=location.href; var url=url.replace(/^(http|https):\/\//i,''); window.open('https://www.widgetfactory.com/index.html/all//'+encodeURIComponent(url)+'/Oc?l=us')})();
Try the following regex url.replace(/\/$/, ""); Exm. below
var url = 'http://www.example.com/';
console.log(url.replace(/\/$/, ""));

inconsistent window.history.pushState uri encoding

Take url address www.somesite.com/#user1
If I click on a good old fashioned <a href... hyperlink containing the link then the # is percent encoded to %40 in the address bar.
If I use html5's window.history.pushstate("object or string", "Title", 'www.somesite.com/#user1') the # is not endocded - it instead shows as a '#' character.
This inconsistency troubles me. Mayhaps there is a way to make the behaviour consistent?
I have considered encodeURIComponent('www.somesite.com/#user1') for the pushstate url, but this also encodes the '/', and what I am hoping is for the <a href... hyperlink not encode the '#' symbol.
Using encodeURIComponent makes javascript assume there are no special HTTP characters to ignore.
extract the compnenet first:
var url = "www.somesite.com/#user1";
var atPos = url.indexOf('#');
var urlComp= url.slice(atPos); //#user1
url = url.slice(0, atPos);
url += encodeURIComponent(urlComp); //"www.somesite.com/%40user1"

What is the difference between "window.location.href" and "window.location.hash"?

I learned "window.location.hash" new and tried in my jquery code instead of "window.location.href" and both of them gave same results.
Code is here :
window.location.href = ($(e.currentTarget).attr("href"));
window.location.hash = ($(e.currentTarget).attr("href"));
What is the difference between them?
For a URL like http://[www.example.com]:80/search?q=devmo#test
hash - returns the part of the URL that follows the # symbol, including the # symbol. You can listen for the hashchange event to get notified of changes to the hash in supporting browsers.
Returns: #test
href - returns the entire URL.
Returns: http://[www.example.com]:80/search?q=devmo#test
Read More
Test it on for example http://stackoverflow.com/#Page
href = http://stackoverflow.com/#Page
hash = #Page
href is the url
hash is only the anchor after the url
http://www.xxxxxxx.com#anchor
http://www.xxxxxxx.com#anchor is the href
"#anchor" is the hash
The hash property returns the anchor portion of a URL, including the hash sign (#).
hash and href are both properties of the window.location object. hash is the part of the URL from the # on (or an empty string if there is no #), while href is a string representation of the whole URL.
Here is the simple example for difference between window.location.href and window.location.hash
For the URL http://www.manm.com/member/#!create:
href: http://www.manam.com/member/#!create
hash: #!create

javascript how to switch pathname of window.location property and redirect

I want to redirect a user from varying urls to a specific one. I've tried various flavors of replacing and I cant seem to get the behavior I want. This code works except I'm providing the hostname. I want to use the existing hostname from windows.location.hostname and just provide a new pathname. Sometimes the urls vary in size and slashes ('/').
window.location = 'http://localhost:36065/NewPath';
How would I change these urls?
http://somesite.com/xxx/yyy/zzz to http://somesite.com/NewPath
http://somesite.com/xxx/yyy to http://somesite.com/NewPath
http://somesite.com/xxx to http://somesite.com/NewPath
I think you get the point. The path can vary in paths, I want to replace everything after .com basically with 'NewPath'
I'd like a clean regex solution if possible but I am quite the rookie in that dept. Thanks for any tips or tricks.
location.pathname = '/newpath.html'
You could always use the various location properties to recreate the part you need and append the new part to it:
window.location = location.protocol + "//" + location.hostname + "/NewPath";
Just to show the hard way:
// Find everything up to the first slash and save it in a backreference
regexp = /(\w+:\/\/[^\/]+)\/.*/;
// Replace the href with the backreference and the new uri
newurl = windows.location.href.replace(regexp, "$1/dir/foo/bar/newpage.html");

Categories

Resources