Hi all,
I'm a newbie of JavaScript. Now I want to call Cooca function from
JavaScript. Below is the code:

//testcontroller.h

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
@interface TestController : NSWindowController {
IBOutlet NSButton * button;
IBOutlet WebView * webView;
}
-(IBAction)loadPage:(id)sender;
-(NSString*)getName;
@end

//testcontroller.m
#import "TestController.h"
@implementation TestController
-(void)awakeFromNib
{
[[webView windowScriptObject] setValue:self forKey:@"JSBridge"];
NSString * path = [[NSBundle mainBundle] pathForResource:@"welcome" ofType:@
"html"];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL
fileURLWithPath:path]]];
}
-(NSString*)getName
{
return @"hi I'm here.";
}
-(IBAction)loadPage:(id)sender
{
NSString * path = [[NSBundle mainBundle] pathForResource:@"welcome" ofType:@
"html"];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL
fileURLWithPath:path]]];
}
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector { return NO; }
+ (BOOL)isKeyExcludedFromWebScript:(const char *)name { return NO; }
@end

//welcome.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Welcome</title>
<style type="text/css">
body {background-color:lightgray;}
</style>
<script language="javascript" type="text/javascript">
function changeIt()
{
var h2s = document.getElementsByTagName("h2");
name = JSBridge.getName();
h2s[0].firstChild.nodeValue = name;
}
</script>
</head>
<body>
<h2 align="center">hello world!</h2>
<input type="button" value="change it" onclick="changeIt();"/>
</body>
</html>

When first time welcome.html is loaded into webView from
-(void)awakeFromNib;, push the "change it" button on web page the
-(NSString*)getName function is called. The "hello world" is replaced with
"hi I'm here.". But when welcome.html loaded again from
-(IBAction)loadPage:(id)sender, push the "change it" button on web page but
NOTHING happens. The "hello world" is no way changed to "hi I'm here.". It
looks that the JSBridge.getName() disappeared.

Could someone explain to me why this happened when the second time
welcome.html loaded? How can I fix this and let the -(NSString*)getName;
function get called again? Any help is appreciated.

-- 
What's happening?
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to