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

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;

Related

# Symbol In Object Key Name Only Works In Chrome?

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

Is this a valid jquery split?

I am using $.cookie('mycookie').split('|')[1] it is working in FF but in IE8 i it is throwing "Object doesn't support this property or method".. Any suggestion?
Here is what i am trying to do,
if($.cookie('mycookie') != null && $.cookie('mycookie').split('|')[1] != '')
A quick look at the plugin indicates that is the expected value if the cookie doesn't exist. The default state is null and then a string if a value is found. Why not just look at document.cookie first yourself:
alert(decodeURIComponent(document.cookie.replace(";","\n\n")));
If the cookie value you are looking for does in fact exist, then I would verify the plugin actually loaded properly:
alert($.cookie.toString());
The get portion of that plugin is about 15 lines so would be trivial to debug if it is the problem. The returned value right now is null, which has no split() method and therefore you are seeing the error in IE as expected.

How to disable specific JavaScript object

HI All,
Is there a way to disable specific JavaScript object, while all other JS objects will be available for execution? Something like a script in the top line which controls the rest of JS code on the page.
For example I want to prevent navigator.userAgent execution and leave navigator.appVersion available.
Or even more advanced way, the execution result must be defined by me. Let's say my browser is FF 3.6.8 but navigator.userAgent would reture IE 8.0
Mostly I'm interested in disabling or superseding objects results that return information about user Browser, Cookie, Resolution and OS
Thanks in Advance.
Jevgenijs
I'm not sure why you would want to do this, but you can override any properties and methods on the window object just by declaring a variable with the same name in the global scope:
var navigator = {
userAgent: "",
appVersion: navigator.appVersion,
// etc...
}
alert(window.navigator.userAgent);
//-> ""
#All: I added following to user.js file ( link text ) and it helped. Now neither my FF browser nor OS type never recognised by any website even by those showed through iframe like adsence.
user_pref("general.useragent.appName", "replacement string for appNameCode");
user_pref("general.appname.override", "replacement string for appName");
user_pref("general.useragent.override", "replacement string for userAgent");
user_pref("general.appversion.override", "replacement string for appVersion");
user_pref("general.platform.override", "replacement string for Platform");
Now it is left to override following screen.width, screen.heigh, screen.colorDepth but these objects seems unable to be overridden via user.js file.
So far only one idea:... most likely FireFox JS engine retrieves these values from OS, hence I need to know which file in Linux (Ubuntu) stores these values and temporary change it when I need. With any WIN OS it would be mush harder to do since it stores everything in damn registries. Am I right ?
Any more ideas ?
Regards

How to determine the Opera browser using JavaScript

I want to determine whether the browser of the client machines is Opera using JavaScript, how to do that?
Now that Opera uses the Chrome rendering engine, the accepted solution no longer works.
The User Agent string shows up like this:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36 OPR/15.0.1147.132
The only identifier for Opera is the OPR part.
Here's the code I use, which should match the old Opera or the new Opera. It makes the Opera var a boolean value (true or false):
var Opera = (navigator.userAgent.match(/Opera|OPR\//) ? true : false);
if(window.opera){
//do stuffs, for example
alert(opera.version()); //10.10
}
No kidding, there is an object opera in opera browser.
You may think, object opera is overridable, but navigator is overridable too.
UPDATE:
To get more accurate result, you could do like
if (window.opera && opera.toString() == "[object Opera]"){
//do stuffs, tested on opera 10.10
}
And I noticed, Opera have both addEventListener and attachEvent, so there is also another way like
if (window.addEventListener && window.attachEvent){
//do stuffs, tested on opera 10.10
}
The above answers no longer work in the new Opera 30. Since Opera now use Chromium. Please use the below:
var isChromium = window.chrome,
isOpera = window.navigator.userAgent.indexOf("OPR") > -1 || window.navigator.userAgent.indexOf("Opera") > -1;
if(isChromium !== null && isOpera == true) {
// is Opera (chromium)
} else {
// not Opera (chromium)
}
The new Opera 30 release now fully uses Chromium and also changed their userAgent to OPR
In Prototype.js, we use this inference:
var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]';
This essentially checks that window.opera object exists and its internal [[Class]] value is "Opera". This is a more solid test than just checking for window.opera existence, since there's much less chance of some unrelated global opera variable getting in the way and resulting in false positives.
Speaking of unrelated global variable, remember that in MSHTML DOM, for example, elements can be resolved by id/name globally; this means that presence of something like <a name="opera" href="...">foo</a> in a markup will result in window.opera referencing that anchor element. There's your false positive...
In other words, test [[Class]] value, not just existence.
And of course always think twice before sniffing for browser. Oftentimes there are better ways to solve a problem ;)
P.S. There's a chance of future versions of Opera changing [[Class]] of window.opera, but that seems to be unlikely.
The navigator object contains all the info you need. This should do:
navigator.userAgent.indexOf("Opera");
do you mind using jQuery?
then you can use jQuery.browser (see documnentation)
But the jQuery-guys recommend not to use this.
We recommend against using this
property, please try to use feature
detection instead (see jQuery.support)
Edit:
For Mootools: use window.opera (see documentation)

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