MrWhetherMan
MrWhetherMan

Reputation: 1334

stringByEvaluatingJavaScriptFromString not being called

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.

Upvotes: 0

Views: 1017

Answers (1)

Christian
Christian

Reputation: 22343

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.

Upvotes: 1

Related Questions