I have a web page I need to manipulate using either DOM or JavaScript, in a Cocoa app.
There’s a link on that web page that triggers a Javascript event.
That link has only a href that points to javascript:doThis('onThat')
When I click that link using my browser, that works just fine, /though/ an error appears on the browser console saying that the variable doThis wasn’t found. The Javascript, however, runs.
When I try to execute javascript:doThis('onThat') on the web page, by using either the evaluate Javascript command of Cocoa or on the browser’s console, it doesn’t work, with the error that the variable doThis isn’t nowhere to be seen… Though, I repeat, it works just fine when I click on the anchor, and the anchor only points to that Javascript code.
Any help would be appreciated.
If you are trying to execute JavaScript code on the Console, you should not be using
javascript:, instead you just try running it by invoking the functiondoThis('onThat'). When you put a piece of JS code in thehrefattribute of an anchor element, then it signals the browser what to do, in this case the browser tries to evaluate that piece of code, and if it finds the function in that webpage it executes it else it just throws up the error.Just try running your code without
javascript:in the Console.