Issue using JavaScript objects in an iOS 7 UIWebView - javascript

I am creating an HTML page to be run inside a UIWebView in iOS 7. When ever I try to read a JavaScript object's property, it breaks. It doesn't crash or anything, just doesn't read the property.
Here is my JS:
var TestObject = {};
var TestObject2 = new Object();
TestObject.title = "This is the first Title";
TestObject2["title"] = "This is the second.";
alert(TestObject.title);
alert(TestObject2.title);
I've tried lowercase object names, passing the property in brackets, and no difference. The alerts read "undefined". This code works in iOS Safari, but not within the UIWebView.
Any ideas on what I'm doing wrong?

I have thousands of problems with dealing with alerts from a uiwebview. If i was you i'd send messages from a JS and catch it with a cocoa selector. try using a library like this one:
https://github.com/tcoulter/jockeyjs
and show the alert from a uialertview.

Related

Parsing of HTMLCollection object in IE 11

I'm having one strange issue in IE 11 while developing custom app that talk to backend service via xml.
The strange thing is that both firefox and chrome can parse the response that I receive, but IE fails,, and I'm completely lost..
If someone can take a look I would be very appreciative.
I'll post example reponse from server,, cuted version of xml doc,, but you'll get the idea
<root>
<wizardhead>
<inputparametar/>
<outputparametar>69439</outputparametar>
<iserror/>
<repeaterror/>
<errormessage/>
<actionstorename>dbo.ncspWizardExec</actionstorename>
<wizardname>459570</wizardname>
</wizardhead>
</root>
the peace of code that parses that xml to extract the values for particular use is here:
window.ro = ro; // var ro is the source XML as string (saving to global var just for ease of testing)
var fnd = document.getElementsByTagName.bind( ro ); // bind 'gebtn' on ro document
var hdd = fnd('wizardhead')[0] ; // reference wizardhead WORKS
var hd2 = hdd.getElementsByTagName('outputparametar')[0].innerHTML; // this FAILS!
the error that I see in developer tools (F12) is on line where I need to compare the hd2 value:
like this => ... (1 == hd2.toString() )
Unable to get property 'toString' of undefined or null reference
Watches panel in devtools show that current env vars are like..
hdd => [object Element]
hd2 => undefined
thanks, oserk
Ok guys,, couple a days after .. I found solution to my problem!
Hope that's gonna help someone with similar issues :)
Reading through w3c docs here: w3c docs I found out that I can reference elements in two ways..
element = collection.item(index)
element = collection[index]
so I applied that to my code ,, for example this line
var hd2 = hdd.getElementsByTagName('outputparametar')[0].innerHTML
to be called like this:
var hd2 = hdd.getElementsByTagName('outputparametar').item(0).textContent
and you see, it worked :)
So I guess IE has some issues with the way it parses and references child nodes with index of maturity > 1 (cose it parses successfully first level!) but got some permission issues inside dom somehow..
OK, lesson learned, use second method instead and we're free of cross browser issues,, for now, at least :)
cheers, k

Invoke javascript class function in firefox addon

I needed xpaths generated by Firepath (Firebug extension), to be passed to my native JavaScript class object present in DOM. So, I am modifying Firepath extension itself, now to pass the generated xpath to my JavaScript class function present in DOM, I can't figure out a way. I tried many solutions like inside the extension function, the following example works:
window.alert("hello");
But the following doesn't:
var pObj = new window.wrappedJSObject.PClass();
alert(pObj);
pObj.CalledFromAddOn();
Any help will be highly appreciated.
After doing some hard work I finally got it working, the document and window objects in Firefox extension refer to different document and window objects and not the DOM (should be obvious), so we need to find the current window to execute the function or class function, whatever. So, here is the code snippet, which you can use in your extension to invoke DOM javascript:
var doc = Application.activeWindow.activeTab.document;
var win = doc.defaultView; // will give you the DOM window object atleast on firefox and chrome
// Now call your functions or create objects
win.wrappedJSObject.hello();
var pToolObj = new win.wrappedJSObject.PTool();
alert(pToolObj.currTaskNo);

jQuery SVG plugin transform animation error

I'm trying to use the jQuery SVG plugin to animate some stuff — scaling and whatnot. I'm totally new to SVG.
var params = {};
params['svgTransform'] = ['scale(1)', 'scale(1.5)'];
$('#TX', svg.root()).animate(params);
This is copied almost verbatim from the developer of the plugin.
Yet when it runs, I'm getting this:
4TypeError: 'undefined' is not a function (evaluating 'f.easing[i.animatedProperties[this.prop]](this.state,c,0,1,i.duration)')
Any ideas?
I think you should check for existence of an element with ID="TX" in your SVG document.
Anyway, I must say that sometimes I found very difficult to remember where to code specific behaviour: there are so many choices, among XML (plain SVG), plain JavaScript+DOM (but what DOM?), jQuery specific, jQuery+SVG.... And all of these with their details... It's daunting! I hope it will be rewarding in the end.
BTW I found that Chrome give a good IDE to workout problems (I'm on Linux now...). Hit Ctrl+Shift+I to enter the debugger and see whatever error...
maybe it doesn't support array inside animate arg object. can you try :
var params = {};
params['svgTransform'] = 'scale(1.5)';
$('#TX', svg.root()).animate(params);

Javascript SetInverval() fireing only once in Firefox [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
setInterval not working (firing only once) in Google Chrome extension
This error only is a problem when document.write() is used.
I am on Mac 10.6.8 Intel.
Here is my script:
setInterval(function(){myFun()},1000);
function myFun() {
document.write('something');
}
The behavior is different in various browsers:
Firefox 12: Only happens once. The browser says "connecting" in the status bar.
Google Chrome and safari: Seems to work correctly.
Note: using setTimeout instead causes everything to behave like firefox (not working).
Using setInterval(myFun},1000), which is supposedly the source of so much error, behaves identically.
Many beginning tutorials use document.write() for "hellow world". However, this funciton is dangerous because it may mess up the script (by nuking the entire program). Here is a safe way to do debug printouts:
Before the script, in between the and in the html, add this:
<div id="myDebug"></div>
In the script, first convert it to a variable that can be called upon:
var myDebug = document.getElementById("myDebug");
When you need to show something, do this:
debug.innerHTML = "some string";
This will show the string in the browser window.
try this:
setInterval(function(){myFun()},1000);
function myFun() {
// Return all elements in document of type 'body'
// (there will only be one)
var bodyArray = document.getElementsByTagName('body');
// get the body element out of the array
var body = bodyArray[0];
// save the old html of the body element
var oldhtml=body.innerHTML;
// add something to the end
body.innerHTML=oldhtml+"somrthing";
}
As others have mentioned, using document.write() will (in some browsers) nuke the script used to update the document, writing to the body (and not the head, where the javascript should be stored) will stop this from happing.
Seems that you miss the semicolon after myFun()
setInterval(function(){myFun();},1000);
function myFun() {
document.write('something');
}
Also make sure you put the function in <body></body>
document.open() results in clearing the document. This function gets called by Firefox before document.write(). Thus your interval is lost.
see W3C Document Object Model HTML

IE Object doesn't support this property or method

This is probably the beginning of many questions to come.
I have finished building my site and I was using Firefox to view and test the site. I am now IE fixing and am stuck at the first JavaScript error which only IE seems to be throwing a hissy about.
I run the IE 8 JavaScript debugger and get this:
Object doesn't support this property or method app.js, line 1 character 1
Source of app.js (first 5 lines):
var menu = {};
menu.current = "";
menu.first = true;
menu.titleBase = "";
menu.init = function(){...
I have tested the site in a Webkit browser and it works fine in that.
What can I do to fix this? The site is pretty jQuery intensive so i have given up any hope for getting it to work in IE6 but I would appreciate it working in all the others.
UPDATE: I have upload the latest version of my site to http://www.frankychanyau.com
In IE8, your code is causing jQuery to fail on this line
$("title").text(title);
in the menu.updateTitle() function. Doing a bit of research (i.e. searching with Google), it seems that you might have to use document.title with IE.
Your issue is (probably) here:
menu.updateTitle = function(hash){
var title = menu.titleBase + ": " + $(hash).data("title");
$("title").text(title); // IE fails on setting title property
};
I can't be bothered to track down why jQuery's text() method fails here, but it does. In any case, it's much simpler to not use it. There is a title property of the document object that is the content of the document's title element. It isn't marked readonly, so to set its value you can simply assign a new one:
document.title = title;
and IE is happy with that.
It is a good idea to directly access DOM properties wherever possible and not use jQuery's equivalent methods. Property access is less troublesome and (very much) faster, usually with less code.
Well, your line 1 certainly looks straight forward enough. Assuming the error line and number is not erroneous, it makes me think there is a hidden character in the first spot of your js file that is throwing IE for a fit. Try opening the file in another text editor that may support display of normally hidden characters. Sometimes copying/pasting the source into a super-basic text-editor, like Notepad, can sometimes strip out non-displayable characters and then save it back into place directly from Notepad.

Categories

Resources