How to access js window object in emscripten's EM_JS method?

Viewed 284

Basically I want to access window.location in a EM_JS method to call this javascript method from c++.

I tried like this

EM_JS(const char*, getlocation, (), {
    let location = window.location;
    let length = lengthBytesUTF8(location) + 1;
    let str = _malloc(length);
    stringToUTF8(location, str, length);
    return str;
});

But getting

ReferenceError: window is not defined

Or is there any other way to get window.location in c++ code compiled with emscripten? But I don't want passing the value of window.location using js to c++ api using ccall or cwrap.

1 Answers

The above code in the question just worked, just that I need to wait until the main is loaded to call this method, it seems.

Related