window.location.search.substring is not working in IE 8 - javascript

Could you solve my question please
window.location.search.substring is not working in IE 8
Regards
Ravindran

Maybe related:
Internet explorer (v9 at least, which is what I'm testing here) doesn't fill location.search when there's a hash (#) in front, and packs everything into location.hash instead.
Here's my workaround:
var query = window.location.search.substring(1);
if (!query) {
var hash = window.location.hash;
query = hash.slice(hash.indexOf('?') + 1);
}

I read that:
Location objects have a toString method returning the current URL. You can also assign a string to window.location. This means that you can work with window.location as if it were a string in most cases. Sometimes, for example when you need to call a String method on it, you have to explicitly call toString
https://developer.mozilla.org/en/DOM/window.location
So I'm thinking:
window.location.search.toString().substring(...) ?

It'll work on Internet Explorer if you change it to document.location.search.substr

Related

IE11 not accepting string(variable) as parameter to localStorage.setItem()

Does anyone knows why IE does not accept a string-variable as a parameter to the method setItem?
Works fine on Chrome.
Example on IE:
This works:
var itemName = 'anyname';
localStorage.setItem(itemName, 'anything');
This doesn´t:
var itemName = 'anyname';
var stringName = 'some string content';
localStorage.setItem(itemName, stringName );
This gives 'Invalid argument error'.
Any help on that?
Thanks! :)
EDIT.
That post relates about another problem, the example given in that post(the one that does not work for him) actually works for me!
My problem shows that the method setItem does not accept a string variable but accepts a normal enclosed quoted string.
Also the solution given in the related post is not acceptable to my problem, I can´t expect that the end user will install a IE11 bug-fix.
After heavy search, the problem lie in the contents of your string. IE11 setItem method does not accept certain chars. My original string´s contents had things like '|' and '~'.
The only workaround I find is to use the encodeURI(yourStringHere) before sending it to the setItem method and after decodeURI it.

How to get anything following the domain in a url, using Javascript

What is the best way to get the "anything" part after the domain part, using Javascript:
http://www.domain.com/anything
http://www.domain.com/#anything
http://www.domain.com/any/thing
For http://www.domain.com/#anything I would have to use window.location.hash. But for http://www.domain.com/anything I would have to use window.location.pathname.
I'm using:
window.location.href.replace(window.location.origin, "").slice(1)
Are there any caveats with this solution? Is there a better way?
Caveats:
location.origin is not supported by IE.
Other improvements: .slice is actually calling Array.prototype.slice. A method call that requires a prototype lookup is bound to be slower than accessing the element you need directly, escpeciallly in your case, where the slice method is returning an array with just 1 element anyway. So:
You could use location.pathname, but be weary: the standard reads:
pathname
This attribute represents the path component of the Location's URI which consists of everything after the host and port up to and excluding the first question mark (?) or hash mark (#).
but I think the easiest, most X-browser way of getting what you want is actually simply doing this:
var queryString = location.href.split(location.host)[1];
//optionally removing the leading `/`
var queryString = location.href.split(location.host)[1].replace(/^\//,'');
It's very similar to what you have now, except for the fact that I'm not using location.origin, which, as shown on MDN is not supported by MS's IE...
Another benefit is that I'm not calling Array.prototype.slice, which returns an array, and requires a prototype-lookup, which is marginally slower, too...
window.location.pathname + window.location.search + window.location.hash
I think this one is a little bit better. You dont have to use any functions here...

window.location.pathname Chrome vs Mozilla

I want to change pathname via function window.location.pathname. I have got this source code.
var hash = window.location.hash;
window.location.pathname = hash;
in Mozilla it works right, but in Chrome doesn't. Chrome write me this adress.
/%23!stranka=novinky&cisloStranky=1&rubrika=novinky&clanek=783?stranka=kontakty#!stranka=novinky&cisloStranky=1&rubrika=novinky&clanek=783
Value of hash is #!stranka=novinky&cisloStranky=1&rubrika=novinky&clanek=783
Have someone any idea?
Thanks.
You'll have to understand that location.hash includes the # itself as well. The rest of the location.hash is, per spec, URLencoded, but the # isn't.
I said "per spec", as Firefox has a bug related to the location.hash property.
If you want to change location.pathname to the hash with the value included, you'll have to encode the # before doing so.
Example:
var hash=location.hash.substring(1)
location.pathname='%23'+hash
If you don't want the hash to be included, just use
var hash=location.hash.substring(1)
location.pathname=hash

How can I detect with javascript if an attribute "#" is appended to an url?

How can I detect with javascript if an attribute "#" is appended to an url ?
I need a cross-browser solution. I'm currently using the following code, but it works only in Firefox:
if ((window.location.hash) || (window.location.hash != "#"))
window.scrollBy(0,-60);
thanks
First, there seems to be an error in your posted code. If you're going for the common string is neither null nor empty nor '#' pattern, it should be:
if (window.location.hash && window.location.hash != "#")
window.scrollBy(0,-60);
That said, since Location.hash is well-supported, the code above should work on all modern browsers.
The following line should do the job for you:
if(!!window.location.hash) // if 'true' you have an appending hash
window.scrollBy(0,-60);
According to w3schools location.hash should work in all major browsers.
If you're finding that the hash property doesn't work in a key browser, you could always just do string parsing, a naive implementation might look like:
var url = window.location.href;
var hashLoc = url.lastIndexOf('#');
if(hashLoc > -1 && hashLoc < url.length )
window.scrollBy(0,-60);
For good cross-browser support you can turn to jQuery. It might be overkill for your project, but overall it makes js developing much easier. There is an excellent hashchange plugin which I have used with great success for a couple of months.

Why can't I use relative URLs with IE7?

I've been Googling for a while and can't seem to find an answer to this question. My problem is as follows:
For my jquery, I need my links to be relative rather than absolute. My PHP is set to return relative urls and everything is working fine, until I test it in IE7. For some reason, IE7 keeps changing my relative urls to abosulute, which breaks my js script. Is this normal? Is there a way to get around it?
For example:
IE8, Chrome, Firefox, Safari etc -
<a href='/page' onclick='click_handler(this);return false;'>clicky</a>
IE7 -
<a href='http://www.myurl.com/page' onclick='click_handler(this);return false;'>clicky</a>
What I do is grab the baseUrl at init, like:
var baseUrl = window.location.href.substring(0, window.location.href.lastIndexOf("/") + 1);
... and then in my URL handler, strip the baseUrl:
var url = $(this).attr("href").replace(baseUrl, "");
Also you can check if the href is "normalized" using .support():
$.support.hrefNormalized
(returns true if the browser makes no modifications when grabbing an href value, so it's currently false in IE.)
If I were you, I'd use browser detection and add a clause to strip the URL down to the relative path. I'm not 100% familiar with jQuery, but I imagine you could do it with a simple split and reconstruction, or REGEX query.
See jQuery URL Parser Plugin: http://plugins.jquery.com/project/url_parser
Looks like it has something to do with security issues - http://blogs.msdn.com/ie/archive/2005/08/15/452006.aspx
Right, it seems the best way to do this is to strip the domain out in jQuery. For anyone that has a similar problem, here is what my click event handler looks like:
var href_attr = element.getAttribute('href');
if (href_attr.indexOf('http://')>-1) {
href_attr = href_attr.substr(location.href.length-1);
};
Here's a modified version of JKS's answer that uses split instead of substring. A little more elegant IMO:
stripBaseUrl : function(url){
var urlArray = url.split("/");
return urlArray[urlArray.length - 1];
}

Categories

Resources