1

I'm not a master of JavaScript and I'm having trouble, so I'm not sure if I'm doing it wrong or what I'm intending is not possible.

In my webpage, I have an iFramed chatbox. The chatbox has 3 text fields, one for username, one for email address, and one for the message. You can see it and inspect its content here: http://appsylvania.com/Chatroom.html

I want to automatically fill the username ("nme") and email ("eml") text fields with a javaScript call. I've inserted a function into the page itself like so:

function fillFields(name,email) {
    var nameText = document.getElementsByName("nme");
    nameText.value = name;
    var emailText = document.getElementsByName("eml");
    emailText.value = email;
}

So, shouldn't this work?

//in UIWebView didFinishLoad delegate method
[webView stringByEvaluatingJavaScriptFromString:@"fillFields('justin','my@mail.com');"];

It doesn't seem to do anything. My guess is that it doesn't work because I'm either doing it wrong or javascript doesn't work on iFrame content.

Thanks for your help.

Daddy
  • 9,045
  • 7
  • 69
  • 98
  • 1
    Per this question http://stackoverflow.com/questions/1088544/javascript-get-element-from-within-an-iframe it appears that you can't access iFrame content from another domain, that is classified as cross-site scripting and is not allowed. – Daddy Mar 22 '12 at 18:23

1 Answers1

1

Did you try accessing via the iFrame?

var ifrm = document.getElementById('cboxmain5-697468');
var nameText = ifrm.getElementsByName("nme");
Alex L
  • 8,419
  • 6
  • 43
  • 51
  • No I didn't because honestly I'm not familiar with javascript other than some online tutorials =). Will try again – Daddy Mar 22 '12 at 17:40
  • ok, didn't work, but i changed a couple things. The chatbox has 2 iFrames, the second iFrame is what holds the textboxes, and it is called "cboxform5-697468". So I changed my script to reflect that. I wish there was an easier way to debug this. – Daddy Mar 22 '12 at 18:04
  • For debugging Chrome has developer tools and javascript console in View>Developer. For firefox you can download Firebug. Then you can easily browse elements and variable console.log(variables). – Alex L Mar 22 '12 at 22:14