質問

This problem only manifests itself when my app runs through the KBX. Running it in a bookmarklet works fine. I used to be able to do this, but it doesn't work any more.

Here's what I have. There's an onclick attribute on a <p> tag that calls a JavaScript function:

<p conclick="window.a163x134_log(this); return false;"></p>

Here's what my function looks like:

emit <|
    window.a163x134_log = function(obj) {
        // Do something
    };
|>;

I've tried using a regular function name (not attached to window) and namespacing it with KOBJ. Neither of those works. The error message I get reads "Uncaught TypeError: Object [object DOMWindow] has no method 'a163x134_log'".

Is this a bug or do I need to change something in my code?

役に立ちましたか?

解決

Sounds like you are running into a sandbox issue. Javascript running in the UBX runs in the sandbox separate from the page.

I have a blog post that may or may not be out of date http://geek.michaelgrace.org/2011/03/kynetxs-new-sandboxed-browser-extensions/

You should also try using the 'use resource' to pull in a javascript file. I believe that gets pulled into the page and not the sandbox.

Also see Kynetx app not working when installed via KBX extension on Chrome

他のヒント

Mike's answer is the most general, but I'm posting my specific solution here for future reference.

The use resource didn't work for some reason. So instead, I dynamically add a <script> tag in an emit like this:

emit <|
    var trigger_click_script = document.createElement("script");
    trigger_click_script.src = "<url to my JavaScript file>";
    document.getElementsByTagName("head")[0].appendChild(trigger_click_script);
|>;

That external JavaScript file then looks like this:

$("p.ttt-time").click(function(e) {
    e.preventDefault();
    // Do something
});

This way, we're attaching the click event handler directly with jQuery, rather than relying on the onclick attribute being able to see a function with a name.

Note: The site I'm working on has jQuery already available, so I'm using their version, not the $K version that come with the Kynetx runtime.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top