# Symbol In Object Key Name Only Works In Chrome? - javascript

ctv.currentdate = new Date(ctv["current"]["#attributes"].attr);
Page works like a charm in Chrome but Firefox, IE10, & Safari all don't work. Firefox console returns the following message:
TypeError: ctv.current['#attributes'] is undefined
If I do console.log(ctv["current"]["#attributes"]);, Firefox returns undefined whereas Chrome returns an actual value, ie. Object {attr: "2013-7-28"}.
Ideas?

Chrome is supporting "#", but that's technically not allowed in ES5. Nice read: http://mathiasbynens.be/notes/javascript-identifiers
Cool validator (if you want to explore further): http://mothereff.in/js-variables

Related

Javascript replace error in Safari on some sites

If you go to https://www.artistguitars.com.au on safari 12 ,open console and enter:
x='%str%';
x.replace (/%str%/g, '$15');
you will get '5' instead of $15
On other browsers it works well.
Why is that? I could not find information anywhere.

String contains not working in IE

I am trying to use contains to find out if a phrase appears within a string
The code below works fine in FF and Chrome, however IE8-10 return an error.
SCRIPT438: Object doesn't support property or method 'contains'
var str = "This is a string";
if(str.contains("string")){
alert('Yes'};
}
Not sure why IE is throwing a error so any help would be much appreciated.
The .contains() function is an ES2015 feature that older Internet Explorer versions don't support.
The MDN page has a polyfill:
if ( !String.prototype.contains ) {
String.prototype.contains = function() {
return String.prototype.indexOf.apply( this, arguments ) !== -1;
};
}
A general guide for questions like this: type MDN something into the Google search box. If you don't find a result, then "something" probably doesn't exist in the JavaScript universe. If you do, then there's a pretty good chance that you'll find the answer you seek there.

How to set "default" value for history.pushState and replaceState?

For browsers that use the title param, what value should we use to tell the browser to use its default?
In Safari 5.1.7 (7534.57.2), if I put null or undefined as the title param, it uses the browser default:
However, Opera 12.16 uses the string "null" and "undefined" respectively:
What's supposed to be the "correct" behavior?
On Opera, how can we set to "default" if "null" and "undefined" doesn't work?
(MDN's History docs doesn't seem to have much info regarding the allowed values for History.pushState/replaceState's parameters.)
Not sure if there is a specific way of setting it to a default title, but a safe way which should work in all browsers is setting the title to the location.href
document.title = location.href;

D3: "Invalid argument" error using d3.js in IE9

I am trying to to get maps working in IE9 using d3.v3.js, world-110m.json and topojson.v1.js (I'm also using jQuery 10.1 so that is loaded too). It works fine in FF and Chrome but, as I am aware, IE 8 and below do not have native SVG support and therefore cannot be supported without workarounds.
Unfortunately, IE9 does not seem to work either. An error is thrown in the IE 9 console:
SCRIPT87: Invalid argument.
d3.v3.js, line 726 character 4
d3_selectionPrototype.insert = function(name, before) {
name = d3_selection_creator(name);
before = d3_selection_selector(before);
return this.select(function() {
return this.insertBefore(name.apply(this, arguments), before.apply(this, arguments));
});
};
Line 726 is "return this.insertBefore" etc. Any ideas what's going on and how to resolve?
IE9 doesn't implement insertBefore quite the same way as other browsers. More detail in the answer to this question I suspect that in your case the second argument is null.

Cross-window Javascript: is there a right way?

Problem
I'm trying to make a method that passes objects to a similar method in a popup window. I don't have control over the code in the target method, or the object passed in. The target method currently serialises the object, using JSON.stringify where possible, or instanceof Array.
The first problem with this is a bug in IE8 (see below). The second, and more fundamental, is that primitives are not the same across windows:
w = open("http://google.com")
w.Array == Array // returns false
Overriding on the popup any classes that might be passed in, and then restoring them after the call works, but it's really brittle and a maintenance headache.
Serialising the object into JSON and then parsing it in the context of the window hits the Firefox bug below.
I'm also a bit loathe to do a deep copy of the object or parse the JSON using new w.Object, etc. because it doesn't feel like it should be that complicated.
Can anyone suggest a sensible way to deal with this, or should I just accept that objects can't be passed verbatim between windows?
IE bug
JSON.stringify doesn't work across windows in IE8. If I pass an object to the popup, which attempts to serialise it, stringify returns undefined. To see this problem, open the script console in IE8 and try:
w = open("http://google.com")
JSON.stringify(Object()) // returns "{}"
w.JSON.stringify(w.Object()) // returns "{}"
w.JSON.stringify(Object()) // returns "undefined" on IE8
JSON.stringify(w.Object()) // returns "undefined" on IE8
JSON.stringify([1, w.Object()]) // returns "[1,null]" on IE8
I tried working around this by setting w.JSON = JSON, but as the last test shows, that breaks when you have objects from both windows.
Firefox bug
It seems that calling w.Object() to create an object in Firefox in fact calls window.Object(). The same bug is hit when calling w.JSON.parse or w.eval. To see this, open Firebug's console and try:
w = open("http://google.com")
new w.Object instanceof w.Object // returns true
w.Object() instanceof w.Object // returns false on Firefox 3.5
w.Object() instanceof Object // returns true on Firefox 3.5
w.Object.call(w) instanceof Object // returns true on Firefox 3.5
w.JSON.parse("{}") instanceof w.Object // returns false on Firefox 3.5
w.JSON.parse("{}") instanceof Object // returns true on Firefox 3.5
w.eval("[]") instanceof w.Array // returns false on Firefox 3.5
w.eval("[]") instanceof Array // returns true on Firefox 3.5
w.eval.call(w, "[]") instanceof Array // returns true on Firefox 3.5
The only workaround I can see is parsing the JSON string myself.
For what it's worth, this is what I'm doing for now:
Ensure jquery-json is loaded in the popup window
Stringify the object
Call w.$.evalJSON(str), which binds primitives correctly
Pass that result to the method on the popup
Alternatively (if jquery-json is not available), you could inject the following script into the target:
<script type="text/javascript">
function parseJSON(j) {
return JSON.parse(j)
}
</script>
as that will capture the popup's JSON, and not the caller's.
Any better solutions gladly appreciated.
If you are trying to do cross-domain scripting, seems like JSONP might be worth investigating.
I can't say I understand your problem fully, but there's an interesting window.name hack that's worth checking out: http://www.sitepen.com/blog/2008/07/22/windowname-transport/ (the blog post uses dojo but, of course, it can be done with pure JS too). It's safer than JSONP, easy to implement and it seems to work on all browsers.
Basically, it allows you to store any data, of any length in the window.name variable. What's awesome is that this variable doesn't get flushed/cleared on page change/refresh, so with some clever use of iframes you get simple and secure cross-domain transport :)
To see this, open Firebug's console
and try:
Error: Permission denied for <stackoverflow> to get property Window.Object from <google>.
On any line except first one: w = open("http://google.com")
Firefox 3.5.7
Think for a moment: You ar trying to open new window with arbitrary site and send to it data available to js. Seems too insecure to allow this.

Categories

Resources