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
Related
If I pass a string as "testabc#" it breaks somewhere on the way route to the controller. I'm not sure why the # is causing me the issue. Is this a key work in JavaScript causing the issue? I have tried all other different specials characters and they work just fine. Has anyone else come across this issue.
`/api/Controller/UpdatePassword?currentPassword=${currentPassword.value}&newPassword=${newPassword.value}&confirmPassword=${confirmPassword.value}`,
Anchor (#) is a feature of URL called fragment and it is used for internal page references. Using a question mark (?) and ampersand (&) can break your HTTP request like an anchor does because all of these things are URL features and it changes URL behavior.
To avoid this kind of error you must use encodeURI() or encodeURIComponent() like below:
with encodeURIComponent():
var url = `/api/Controller/UpdatePassword?currentPassword=${encodeURIComponent(currentPassword.value)}&newPassword=${encodeURIComponent(newPassword.value)}&confirmPassword=${encodeURIComponent(confirmPassword.value)}`
with encodeURI():
var url = encodeURI(`/api/Controller/UpdatePassword?currentPassword=${currentPassword.value}&newPassword=${newPassword.value}&confirmPassword=${confirmPassword.value}`)
# in a url is a special character which indicates a "fragment", which means a special part of the url which is an anchor in the page, of sorts. Thats why it breaks your code. ? And & are also special, and so are a few others.
You should url encode data that you pass in the url.
Try use encodeURI() to the url string before fetching, like:
var url = `/api/Controller/UpdatePassword?currentPassword=${currentPassword.value}&newPassword=${newPassword.value}&confirmPassword=${confirmPassword.value}`
encodeURI(url)
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
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
I have a Spring-MVC application with Freemarker as the view component.
In my templates, several links are generated which point back to my application and which include URL parameters containing a hash key (#).
Example:
parameter: Q#106368 11
URL generated by Freemarker with encoded param: testurl.html?key=Q%23106368%2011
I use JavaScript to redirect to this URL (reason: I use JS to manage loading of 2 frames at the same time).
The redirect method is simple:
function redir(url) {
window.location.href = url;
}
The JS call generated by Freemarker looks like
test
My problem is that the browser / Javascript converts back the URL encoded parameter, thinks there is a # and cuts off there.
When I use window.location.href='http://...' directly it works. Only when using the method parameter it seems to be magically URL decoded and then the redirect fails because the URL gets cut off at the #.
Is there an easy way to transmit the parameter correctly?
I am aware that I could replace the #, e.g. with $$$hash$$$, in the template and do the replacement on the server side again. But there are so many places I would have to change...
As Marc B commented, it is necessary to URL encode again. The method would be encodeURI(). However, this method does not encode the # sign. For my specific use case, I have to replace the # sign with %23 after the encoding.
The redirect JS method finally looks like:
function redir(url) {
url = encodeURI(url);
url = url.replace(/#/g, '%23');
window.location.href = url;
}
Comparing escape(), encodeURI(), and encodeURIComponent()
encodeURIComponent/decodeURIComponent is more thorough than just encodeURI, it will decode/encode '#' and event '/'
What browser are you using? I'm trying FireFox 5 and it doesn't convert %23 back into # in my testing. When you mouse over the link or copy the link location, what does that have? Are you sure whatever is outputting the link isn't doing the conversion?
Update
This isn't ideal, but it seems like it solves the problem:
<a onclick="url = 'http://localhost:8080/testappp/testurl.html?key=Q%23106368%2011';" href="javascript:redir(url);">test</a>
It seems like the href attribute is decoded. When I mouse over it I seen the # instead of the %23.
I have a url which looks like this
https://test.high.com/people/11111111-name-firstname-_custa/deals/new
Now i need to match document.URL
if im on that Page if so i will alert a message.
The important part is /deals/new
How can i match that in Javascript?
var regex = new RegExp("/deals/new$");
if(document.URL.match(regex))
alert("yeah");
A Regex matching any string ending with "/deals/new" is
"/deals/new$"
If you need your link to only contain /deals/new, try
"/deals/new(/|$)"
Do you mean that you want access to the documents url from javascript?
That is done via the documents location property.
Try document.location.href or only location.href.