0

I know problems like this have been posted here, but I looked through the previous threads and wasn't able to find an answer. I am trying to use stringByEvaluatingJavaScriptFromString in swift, and it is not working. Please help! Code:

import UIKit
import Foundation
import iAd

class zmanimViewController: UIViewController, ADBannerViewDelegate, UIWebViewDelegate {
//variables*******************************************
var adBannerView = ADBannerView(adType: ADAdType.Banner)
@IBOutlet weak var webView: UIWebView!

//functions*******************************************


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.canDisplayBannerAds = true
    webView.delegate = self

    var url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("testHTML", ofType: "html")!)
    loadLocalHtmlFile(webView, url!)

    //var jsZoom25 = "alert(\"ran\");"

    //changeWebViewFontSize(2, webView)


}
func webViewDidFinishLoad(webView: UIWebView) {
    //changeWebViewFontSize(2, webView)
    var jsZoom25 = "alert(\"ran1\"); document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust=450%; alert(\"ran2\");"
    webView.stringByEvaluatingJavaScriptFromString(jsZoom25)

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

Thanks in advance! Additional info: If I make the javascript only "alert('ran');", it shows the alert, but it does't show the alerts in the javascript that I need to work.

2
  • what do you mean with 'not working'? is there an error? Commented Feb 5, 2015 at 17:58
  • The javascript is not being called. Commented Feb 5, 2015 at 18:54

1 Answer 1

1

Your problem is at your 450%. You need to write these in (double)quotes: '450%'.

Also I'd recommend to use single quotes in your string, because then you don't have to escape every double quote.

var jsZoom25 = "alert('ran1'); document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust='450%'; alert('ran2');"

If you have to do something with Javascript again, check the syntax first. You can do that online with for example Esprima.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.