IE9 javascript replace does not work - javascript

I have a string such as "Value = $val$" and I want to replace $val$ with some value like "$0.00"
so final string can look like "Value = $0.00"
This works in firefox and IE 7 and IE 8 but does not work in IE 9 any idea why? and how I can resolve the issue?
Any value other than $0.00 (e.g.$5.00) works without any issue.
http://jsfiddle.net/jhdVV/5/
edit: updated link with a textbox and a button to test with diff values.
http://jsfiddle.net/jhdVV/10/
In IE 9 I am getting "Value =$val$.00"
Browser is in Standards mode.
Note:
I am working on legacy code so , ideally I would like to stay away from tempting jquery solutions.

Any reason you're not simply doing this?
function replaceValue(source, find, replacement) {
return source.replace(find, replacement);
}

The $0 in your replacement text is essentially an uninitialized variable, the behavior of which is undefined. So, escape the dollar sign:
$$0.00

Related

'target.html()' is null or not an object

I'm trying to replace all occurrences of a certain character (quotes) from an element. My code works fine in Chrome and FF but fails in IE with the debugger saying - 'target.html()' is null or not an object
here is what my code looks like -
text = "some random text";
target = $('#target');
target.append(text);
target.html(target.html().replace(/"/g, " "));
What's causing that error in IE and how do i fix it?
'target' is used an attribute and IE does not like it if you use it as a variable name. In fact it even refuses to recognize event.target and insists on event.srcElement (tell me about it ..) .
Anyways, it should work if you rename the object to $target.
One of the main reasons I get such errors is because the HTML is not correctly formed(and it sure gave me hell before I found out). Other browsers allow a missing '>' or other syntactical errors, but IE is very strict.
So just see minutely that the markup in target.html() is correct.

Javascript can't create dictionary in IE

var Dictionary = {
foo: "bar"
}
alert(Dictionary.foo)
This works fine in Firefox 6, but not in IE8, is there a syntax error that FF is displacing, or do I need an IE fix?
This example does work in IE8 - at least for me. I'm running on a Windows XP SP 3 with the latest IE8.
If you have problems with some Object-notations, check if your key is named like keywords which are specified in JavaScript, like class and aren't defined as a String (you need to write it as "class". Otherwise missing colons are often a source of errors.

Why does Internet Explorer not like this jQuery?

While debugging some jQuery that is not working in IE, I found this error message:
var item = $("#item_"+ itemArray[itemIndex]).find('a').text().trim();
Object doesn't support this property or method (script.js, line 100, character 2)
The character 2 doesn't make sense to me. Based on the text displayed character 2 would be the letter a in var but of course that doesn't make any sense.
(Should I not use var?)
I know that jQuery is working to some extent or the script would not have been able to get this far on my page.
IE doesn't have String.trim(), you'll need $.trim() (which uses native trim if available, emulates it in IE), like this:
var item = $.trim($("#item_"+ itemArray[itemIndex]).find('a').text());
IE doesn't have a trim method.
Instead, you can call jQuery.trim(...).

ie8 var w= window.open() - "Message: Invalid argument."

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.

Why is a matched substring returning "undefined" in JavaScript?

I came across a strange behaviour when doing some regular expressions in JavaScript today (Firefox 3 on Windows Vista).
var str = "format_%A";
var format = /(?:^|\s)format_(.*?)(?:\s|$)/.exec(str);
console.log(format); // ["format_%A", "%A"]
console.log(format[0]); // "format_undefined"
console.log(format[1]); // Undefined
There's nothing wrong with the regular expression. As you can see, it has matched the correct part in the first console.log call.
Internet Explorer 7 and Chrome both behave as expected: format[1] returns "%A" (well, Internet Explorer 7 doing something right was a bit unexpected...)
Is this a bug in Firefox, or some "feature" I don't know about?
This is because console.log() works like printf(). The first argument to console.log() is actually a format string which may be followed with additional arguments. %A is a placeholder. For example:
console.log("My name is %A", "John"); // My name is "John"
See console.log() documentation for details. %A and any other undocumented placeholders seem to do the same as %o.
Seems like %A somehow translates into the string undefined.
Try escaping the %A part, I think that will solve the problem.

Categories