admin管理员组文章数量:1401787
Somehow window.location.hash is being handled differently in different browsers. If I have a url as follows
#parcel/history/1?as=json&desc[]=ctime&desc[]=history_id
and I am interested in getting values in between #parcel/history/ and ?as=json ... so the substring statement would be something similar to
window.location.hash.substring(14, window.location.hash.search(/\?/g));
I have that work in firefox 3.0.10 without problem but the same substring statement doesn't work in Opera 9.60.
After some quick searching, I found some interesting info that may help
- window.location.hash should always return urlencoded string, but this is a bug in Firefox
If the hash part of the URL contains encoded characters (see Core_JavaScript_1.5_Reference:Global_Functions:encodeURIComponent), hash returns the decoded URL part. This is a bug in Firefox. href, search and pathname return the correct, encoded URL parts.
- Opera returns only #parcel/history/1 and ignores the remaining string, and this is the main reason why my substring statement failed...
Is there a better way if I want to extract the string between #parcel/history/ and ?as=json.... besides regular expression?!
Somehow window.location.hash is being handled differently in different browsers. If I have a url as follows
http://maps-demo.bytecraft..my/postdemo/parcel
#parcel/history/1?as=json&desc[]=ctime&desc[]=history_id
and I am interested in getting values in between #parcel/history/ and ?as=json ... so the substring statement would be something similar to
window.location.hash.substring(14, window.location.hash.search(/\?/g));
I have that work in firefox 3.0.10 without problem but the same substring statement doesn't work in Opera 9.60.
After some quick searching, I found some interesting info that may help
- window.location.hash should always return urlencoded string, but this is a bug in Firefox
If the hash part of the URL contains encoded characters (see Core_JavaScript_1.5_Reference:Global_Functions:encodeURIComponent), hash returns the decoded URL part. This is a bug in Firefox. href, search and pathname return the correct, encoded URL parts.
- Opera returns only #parcel/history/1 and ignores the remaining string, and this is the main reason why my substring statement failed...
Is there a better way if I want to extract the string between #parcel/history/ and ?as=json.... besides regular expression?!
Share Improve this question asked May 5, 2009 at 9:19 Jeffrey04Jeffrey04 6,33812 gold badges48 silver badges70 bronze badges 3- 1 Never seen anchor tags abused at that magnitude before, wow – Chad Grant Commented May 5, 2009 at 9:28
- heh quite :) - worth pointing out – annakata Commented May 5, 2009 at 9:44
- @Deviant heh, you will be amazed to see vmware server 2.0 uses hash that looks like this 127.0.0.1:8333/ui/#{e:%22HostSystem|ha-host%22,w:{t:true,i:0}} – Jeffrey04 Commented May 6, 2009 at 2:34
4 Answers
Reset to default 4Try this:
var match = window.location.href.match(/^[^#]+#([^?]*)\??(.*)/);
var hashPath = match[1];
var hashQuery = match[2];
This matches the following parts of the hash:
…#parcel/history/1?as=json&desc[]=ctime&desc[]=history_id
\______________/ \____________________________________/
hashPath hashQuery
This is my current solution to the problem
var get_hash_end = function(_hash) {
var result = _hash.length;
if(_hash.search(/\?/g) != -1) {
result = _hash.search(/\?/g);
}
return result;
};
You could just use window.location.href
instead of hash but why not use regex? More reliable and future-safe than a method based on substringing from character N. Try:
window.location.href.match(/#parcel\/history\/(.*?)(\?|$)/)[1]
Regex isn't the right answer all the time, but sometimes it's just right.
Edit: cleaned up method encapsulation
function GetValue()
{
var m = window.location.href.match(/#parcel\/history\/(.*?)(\?|$)/);
return m ? m[1] : null;
}
You could also do that :
var whatYouWant = window.location.hash.split('?')[0].substring(14);
本文标签: javascriptdoing substring in windowlocationhashStack Overflow
版权声明:本文标题:javascript - doing substring in window.location.hash - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744272719a2598261.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论