IE7 JSON object being appended to basepage url - javascript

I'm having strange IE7 behavior at the moment. We reach out to an external service that returns a formatted JSON object back to us. Any other version of IE can handle this object. However in IE7, it simply appends the JSON string to the end of the basepage url. so we end up with something like this...
http://localhost:1111/X?Param=223"#13988652796891&{"TestCodes":{"TestCode":.....
The page then takes it upon itself to refresh, and breaks because the url isn't correct. Has anyone seen anything like this before, and if so was there something they were using that caused this to happen? I can give a few more details, I have other issues debugging because I do not have an actual instance of IE7 on this machine. (I've been using IETester, but I can't actually step through code this way)
Thanks in advance!

Related

Set content type based on browser

I am using asp.net mvc 3 and returning json to the user. I'm doing that with:
return Json(results, JsonRequestBehavior.AllowGet)
results is just a simple viewmodel c# class I created. That works fine in FF and chrome, but then IE 9 asks to open or save the results. Everywhere I look, people say the "fix" is to do something like:
return Json(results, "text/html", JsonRequestBehavior.AllowGet)
This does work for me in IE, but it doesn't work in chrome and/or firefox. It does for some versions, but not all. I was wondering if it's possible to return text/html if the browser is IE, otherwise return the normal JSON. Or is there a better solution? Thanks in advance!
I am assuming that you are doing an AJAX call, if so the solution you have is the correct way to make ALL browsers treat the returned values as HTML, which is a much better solution than dealing with each browser in a different way.
However, in order to be able to work with the returned values, you will need to parse the returned html into JSON.
In the success callback of the AJAX call, simply add the following:
var jsonResponse = $.parseJSON(response);

Javascript Date object failing

On just one page of my site the javascript Date object won't work. Calling Date.parse(dateString) or new Date(dateString) returns undefined instead of the expected Date object. In fact on this page, typing this command into the firebug console will crash the entire browser (this command being something like Date.parse("6/1/2013") or Date.parse("Jun 6, 2013") ). This only happens on this one page and these same commands work just fine in every other page. I've looked for anywhere that the Date object and functions might have been overwritten but couldn't find anything. Does anyone know what might be causing this error?
First of all - does this happen in any browser? or is it specific to one?
The most likely scenario is that you have some plug-in (Java applet, Flash, Silverlite or something else entirely) on that one page which overwrites the built-in Javascript Date type with something else.
I would start looking at what is different on that page from the other pages in terms of plugins loaded and other .js files that are loaded only on that page and not on the others.
Date objects are pretty flexible, they are more apt to return NaN from bad input than undefined. Sounds like you redefined Date- you don't have an expression that has 'Date='anywhere, do you?

Avoid FF JS automatic HTML encoding?

I'm trying to make simple templating for users on a site. I have a test line like this :
<div id="test">Test</div>
It will alert the HTML properly with the following JS in all browsers except FF:
alert( document.getElementById( 'test' ).innerHTML );
In FF it will change the curly braces to their HTML encoded version. I don't want to just URL decode in case the user enters HTML with an actual URL instead of one of the templated ones. Any ideas to solve this outside of REGEXing the return value?
My fiddle is here: http://jsfiddle.net/davestein/ppWkT/
EDIT
Since it's seemingly impossible to avoid the difference in FF, and we're still early in development, we are just going to switch to using [] instead of {}. Marking #Quentin as the correct answer since it's what I'm going by,
When you get the innerHTML of something, you get a serialised representation of the DOM which will include any error recovery or replacing constructs with equivalents that the browser does.
There is no way to get the original source from the DOM.
If your code won't contain %xx elsewhere, you can just run it through unescape().

view contents of large array?

I have a very large array I need to check for debugging purposes, problem is it crashes firebug and the like if I try to view the data.
Is there a way I can dump the array to a text file or something?
Why not just dump it on the document itself? If you are using Firefox, try the following:
document.write(myBigArray.toSource());
Then copy paste like your usually do on normal website.
p/s: toSource() requires browser that supports Javascript 1.3 and above
Opera has scrollable alerts, it's very useful for developing.
EDIT: Tested with success for messages with 500000 lines. You can also copy from it.
Post the array to the server (json/hidden field normal form post), and use your server-side language to save that array dump to a file.
If you are using IE you could try copying a string representation of the array to the clipboard.
There are some libraries that can help in writing to files. You can use ActiveX, but that binds you to internet explorer for your debugging and that's kind of outside the javascript world.

Getting URL of executing JavaScript file (IE6-7 problem mostly)

Hey all, I've been trying to throw together a generic function that retrieves the absolute URL of an executing JavaScript file on a web page:
http://gist.github.com/433486
Basically you get to call something like this:
getScriptName(function(url) {
console.log(url);
// http://www.example.com/myExternalJsFile.js
});
inside an external JavaScript file on a page and can then do something with it (like find the <script> tag that loaded it for example).
It works great in almost all the browsers I've tested (Firefox, Chrome, Safari, Opera v10 at least, and IE 8).
It seems to fail, however, in IE 6 and 7. The callback function gets executed, but the retrieved name is the URL to the main HTML page, not the JavaScript file. Continuing with the example, getScriptName invokes the callback with the parameter: http://www.example.com/index.html
So all I'm really asking is if there's some other way of getting the URL of the current JavaScript file (which could be IE 6 and 7 specific hackery)? Thanks in advance!
EDIT: Also, this won't work in every case, so please don't recommend it:
var scripts = document.getElementsByTagName("script");
return scripts[scripts.length-1].src;
I'd like it to work in the case of dynamically created script tags (possibly not placed last in the page), aka lazy-loading.
A lot of this depends on what you have access to. If, as it appears, you are trying to do this entirely within the JS code, I do not believe that you are able to do it, for some of the reasons shown above. You could get 90% of the way maybe, but not be definitive.
If you are working in a dotnet environment ( which is the only one I know ), I would suggest the use of a module that would intercept all JS requests and add into them the request location, or something of that nature.
I think you need to address this from the server side, not the client side. I do not think you will have a definitive answer form the client side. I think you will also struggle to get an answer from the server side, but you might be more successfull.
Sorry, I suspect you might struggle with this. IE earlier than version 8 typically gives error messages from javascript errors of the form:
line: 342
char: 3
error: expected identifier, string or number
code: 0
url: http://example.com/path/to/resource
where the url is the window.location.href, rather than the URL of the external Javascript resource that contains the problem. I suggest that IE gives the unhelpful URL value since the script URL isn't available to IE at that point, and neither is it available to any Javascript you might write to try to display it.
I would love to be able to link to IE8 release notes which say this bug / feature has been fixed, hence the reason I created this as community wiki. My MSDN foo is pretty weak!

Categories

Resources