I have some JavaScript for a dropdownlist to sort results on a product inventory page. In Internet Explorer the sorting works fine and the browser handles this perfect. However in Chrome it fails every time (can you believe it, something works in IE but not Chrome?)
In IE when I use the Sort By option the URL looks like this:
MyExampleSite.com/Supplies/Products/12345/MyProduct/?a=0
However when I do the Sort By option in Chrome here is what the URL looks like:
MyExampleSite.com/Supplies/Products/12345/MyProduct/?&a=0
As you can see it adds the amp in the URL, and if I keep trying to sort it just add's an additional amp everytime.
Here is the JavaScript which caused my issues:
$("[name=a]").change(function () {
window.location = '#(this.Model.SortUri)' + '#(this.Model.SortUri.IndexOf('?') == -1 ? "?" : "&")a=' + this.value;
});
My Solution was to add Html.Raw like this:
$("[name=a]").change(function () {
window.location = '#(this.Model.SortUri)' + '#Html.Raw(this.Model.SortUri.IndexOf('?') == -1 ? "?" : "&")a=' + this.value;
});
And suddenly it works fine in IE and Chrome.
My question is why did Chrome do this but not IE?
Well, you need to make sure all your stuff is encoded correctly, and I'm guessing you aren't. We would need to see much more of your page to determine that. IE is probably detecting that you've done it incorrectly and attempting to fix it for you, while Chrome isn't auto-fixing your mistake.
It gets pretty complicated as to the rules of when you do and don't need to escape stuff. It's in a HTML page, and everyone knows you need to escape & in HTML pages, but it's in a script tag, so do you or don't you need to escape it? Well that depends if you also have the script tag in a CDATA element or not.
The simple solution is to avoid doing that. Put the URL you want to switch to on the [name=a] element as a data tag like so:
<sometag name='a' data-urlprefix='#(this.Model.SortUri)#(this.Model.SortUri.IndexOf('?') == -1 ? "?" : "&")a='>stuff</sometag>
Then in your javascript:
$("[name=a]").change(function () {
window.location.href = $(this).data('urlprefix')+encodeURIComponent(this.value);
});
This also has the benefit of moving the server processing stuff to just the HTML parts, so that you can put the javascript in it's own file, which you should be doing anyhow.
Note that you should be urlencoding this.value if it isn't already as well, so I've done that. If it is already encoded, you can safely remove it. I've also changed window.location to window.location.href because window.location can do strange things sometimes as well -- encoding on some browsers, but not on others, etc.
You just need to make the entire string as part of your Html.Raw(build ou the logic outside and directly insert that variable here)
Please check this post
Why is Html.Raw escaping ampersand in anchor tag in ASP.NET MVC 4?
jQuery("input[name=a.b.c]")
Executing this line using jQuery 1.10.2 or 1.9.1 results in the message:
"Syntax error, unrecognized expression: input:hidden[name=a.b.c]".
I understand the core problem which is that the dots are not escaped or quoted out. This would work:
jQuery("input[name='a.b.c']")
The constraint is that I do not have the ability to change the line of code with the bad selector. That line is produced by the website (which I don't own) and they don't give me the ability to change that.
However, they do allow me to add arbitrary JS files to the header of the page (which means I can use a different jQuery version or even edit the jQuery file). My question is whether anyone knows another way around this so that jQuery can cope without the quotes since I cannot change the bad code.
For those saying that I can just change the name, this doesn't help because the JS still throws an error because changing the name of the element doesn't fix the bad selector.
Thanks
The proper way of executing this selector is:
jQuery('input[name="a.b.c"]')
Obviously you need to edit the algorithm that creates this line, there's no way jquery will accept an invalid selector.
Take a look here.
How do I extend jQuery's selector engine to warn me when a selector is not found?
In your case I would do something like this.
var oldInit = $.fn.init;
$.fn.init = function(selector, context, rootjQuery) {
selector = fixItWithQuotes(selector, context, rootjQuery);
return new oldInit(selector, context, rootjQuery);
};
untested by me, but it should give you an idea.
Also, this might give you more ideas?
http://blog.tallan.com/2012/01/17/customizing-the-default-jquery-selector-behavior/
Hope that makes sense.
Why don't you change the name attribute yourself?
var el = $("input");
el.attr("name", el.attr("name").replace(/[\d\.]+/g, ""));
console.log(el.attr("name"));
Then change it back if you need to. jsFiddle here
I'm clueless.
In my Jquery Mobile Plugin I'm declaring:
var $currentEntry = $.mobile.urlHistory.stack[$.mobile.urlHistory.activeIndex].url;
$activePage = $('div:jqmData(url="'+ $currentEntry +'")');
So I'm taking the active page's url and use it to construct an $activePage object.
This works fine on desktop, but on my iPad (iOS3.3), $currentEntry is defined correctly, but $activePage is undefined.
Question:
What can be reasons for this?
You can rule out race conditions, because wrapping this in a 10sec timeout still produces the same result. Also, if I console the respective page directly and query it's data-url, it shows the correct value. So how come the above still gives me undefined on iOS
undefined
while working correctly everywhere else?
Thanks for any hints!
EDIT:
The element will be dynamic, but I can console for the page in my setup directly like so:
console.log( $('div:jqmData(wrapper="true").ui-page-active').attr('id') );
console.log( $('div:jqmData(wrapper="true").ui-page-active').attr('data-url') );
Both return the correct id and data-url, so the elements must exist.
EDIT2:
I can query for the attribute data-url which gives me the correct value. However, I cannot select using this attribute like so:
$('div[data-url="'+$currentEntry+'"]').length
which gives me 0
I am going to admit that I am blind-guessing, but you should try:
$activePage = $('div').filter(function(){return $(this).jqmData('url') === $currentEntry})
BTW, just for semantics i think "$currentEntry" shouldn't start with a dollar sign if it is not a jQuery object.
I have a site that has an IE8-only problem:
The code is:
var w = window.open(urlstring, wname, wfeatures, 'false');
The error is:
Message: Invalid argument.
Line: 419
Char: 5
Code: 0
URI: http://HOSTNAME/js_context.js
I have confirmed the line number of the code (the "Line" and "URI" are correct), and I understand in later versions of IE8, this is considered accurate.
I have checked all the incoming parameters in the call by dumping alerts, and they all look valid.
This problem does not happen on FF (probably 3).
UPDATE:
The problem appears to be in using assigning the result of window.open() when doing "var w". When I split the line into two statements it works in IE8.
UPDATE2:
Based on:
http://javascript.crockford.com/code.html
When a function is to be invoked
immediately, the entire invocation
expression should be wrapped in parens
so that it is clear that the value
being produced is the result of the
function and not the function itself.
This is not exactly what is going on here, but I found that applying the principle solved the problem, in IE8's compatability mode.
var w = (window.open(urlstring, wname, wfeatures, false));
This is an old posting but maybe still useful for someone.
I had the same error message. In the end the problem was an invalid name for the second argument, i.e., I had a line like:
window.open('/somefile.html', 'a window title', 'width=300');
The problem was 'a window title' as it is not valid. It worked fine with the following line:
window.open('/somefile.html', '', 'width=300');
In fact, reading carefully I realized that Microsoft does not support a name as second argument. When you look at the official documentation page, you see that Microsoft only allows the following arguments, If using that argument at all:
_blank
_media
_parent
_search
_self
_top
IE is picky about the window name argument. It doesn't like spaces, dashes, or other punctuation.
When you call window.open in IE, the second argument (window name) has to be either one of the predefined target strings or a string, which has a form of a valid identifier in JavaScript.
So what works in Firefox: "Job Directory 9463460", does not work in Internet Exploder, and has to be replaced by: "Job_Directory_9463460" for example (no spaces, no minus signs, no dots, it has to be a valid identifier).
the problem might be the wname, try using one of those shown in the link above, i quote:
Optional. String that specifies the
name of the window. This name is used
as the value for the TARGET attribute
on a form or an anchor element.
_blank The sURL is loaded into a new, unnamed window.
_media The url is loaded in the Media Bar in Microsoft Internet
Explorer 6. Microsoft Windows XP
Service Pack 2 (SP2) and later.
This feature is no longer
supported. By default the url is
loaded into a new browser window or
tab.
_parent The sURL is loaded into the current frame's parent. If the frame has no parent, this value acts as the value _self.
_search Disabled in Windows Internet Explorer 7, see Security and Compatibility in Internet Explorer 7 for details. Otherwise, the sURL is opened in the browser's search pane in Internet Explorer 5 or later.
_self The current document is replaced with the specified sURL.
_top sURL replaces any framesets that may be loaded. If there are no framesets defined, this value acts as the value _self.
if using another wname, window.open won't execute...
Actually a name can be used however it cannot have spaces so
window.open("../myPage","MyWindows",...) should work with no problem (window.open).
I also meet this issue while I used the following code:
window.open('test.html','Window title','width=1200,height=800,scrollbars=yes');
but when I delete the blank space of the "Window title" the below code is working:
window.open('test.html','Windowtitle','width=1200,height=800,scrollbars=yes');
Hi using the following code its working...
onclick="window.open('privacy_policy.php','','width=1200,height=800,scrollbars=yes');
Previously i Entered like
onclick="window.open('privacy_policy.php','Window title','width=1200,height=800,scrollbars=yes');
Means Microsoft does not allow you to enter window name it should be blank in window.open function...
Thanks,
Nilesh Pangul
For me the issue was with a dash "-" in the window name field. I removed the dashes and IE does not error out and in fact opens the window.
What does position four represent, the one that has 'false' as an value? Shouldn't that be false, (i.e. without quotes?). It's possible that earlier versions of IE would coerce the string to a boolean, but newer ones don't.
The answers here are correct in that IE does not support spaces when setting the title in window.open(), none seem to offer a workaround.
I removed the title from my window.open call (you can use null or ''), and hten added the following to the page being opened:
<script>document.title = 'My new title';</script>
Not ideal by any means, but this will allow you to set the title to whatever you want in all browsers.
Try remove the last argument. Other than that, make sure urlstring, wname, and wfeatures exist.
I discovered the same problem and after reading the first answer that supposed the problem is caused by the window name, changed it : first to '_blank', which worked fine (both compatibility and regular view), then to the previous value, only minus the space in the value :) - also worked. IMO, the problem (or part of it) is caused by IE being unable to use a normal string value as the wname. Hope this helps if anybody runs into the same problem.
If you want use the name of new window etc posting a form to this window, then the solution, that working in IE, FF, Chrome:
var ret = window.open("", "_blank");
ret.name = "NewFormName";
var myForm = document.createElement("form");
myForm.method="post";
myForm.action = "xyz.php";
myForm.target = "NewFormName";
...
It seems when even using a "valid" custom window name (not _blank, etc.) using window.open to launch a new window, there is still issues. It works fine the first time you click the link, but if you click it again (with the first launched window still up) you receive an "Error: No such interface supported" script debug.
This only happens with IE (all versions), on line 1120 in
jquery-1.2.6.js I get the following error:
Line 1120:
Invalid Property Value
The line in the js file is the following:
elem[name] = value;
It is inside attr: function( elem, name, value )
Does anybody have a problem similar to this?
If this is also you, it sounds like you're trying to change the CSS of the element rather than give it an attribute.
If that is the case then try this instead;
jQuery.css('color', 'inherit');
This error can also occur if you call jQuery.css with an invalid attribute value, such as:
$('div.foo').css('padding-left', 'NaNpx');
The problem is IE-only because you are probably trying to set something like "min-height" which exists in (a proper CSS implenting) browser like Firefox, but not in a (demon spawned fiend of a) browser like Internet Explorer. I ran in to the same issue using jQuery's own dialog UI function.
I was a huge proponent of jQuery before this, but this has really put some egg on its face.