Loading .js file is not working

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Loading .js file is not working

Postby mutulong » Tue Apr 15, 2014 8:00 pm

I'm in the process of integrating CEF3 into my current project as there are increasingly javascript functions I am adding, it makes sense to put all javascript functions into a separate .js file, and load it into html as

<script type="text/javascript" src="http://internal/helper.js"></script>

However, it doesn't work for some reason and I have tried without the custom link like

<script type="text/javascript" src="helper.js"></script>

and still received the same error - ["Uncaught ReferenceError: showAlert is not defined", source: http://internal/tests.html

My code is built on top of cefsimple example, and there is no custom scheme has been defined, I have a method to load resource based on mine type (ParseTestUrl method), that's why loading http://internal/index.html works fine

Code: Select all
CefRefPtr<CefResourceHandler> SimpleHandler::GetResourceHandler(
                                                                CefRefPtr<CefBrowser> browser,
                                                                CefRefPtr<CefFrame> frame,
                                                                CefRefPtr<CefRequest> request) {
    std::string url = request->GetURL();
    if (url.find(kInternalOrigin) == 0) {
        // Handle URLs in the test origin.
        std::string file_name, mime_type;
        if (ParseTestUrl(url, &file_name, &mime_type)) {
            // Load the resource from file.
            CefRefPtr<CefStreamReader> stream =
            GetBinaryResourceReader(file_name.c_str());
            if (stream.get())
                return new CefStreamResourceHandler(mime_type, stream);
        }
    }
    return NULL;
}


Could anyone shed some light on what I did wrong? What would be the best way to achieve my goal without having to define custom scheme?

Thanks in advance.
mutulong
Newbie
 
Posts: 2
Joined: Tue Apr 15, 2014 7:57 pm

Re: Loading .js file is not working

Postby magreenblatt » Tue Apr 15, 2014 8:38 pm

It looks like an error in your JavaScript code. You can use DevTools to debug the problem.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Loading .js file is not working

Postby mutulong » Wed Apr 16, 2014 4:54 am

Thanks for the reply.
Here is the code I have in javascript file and html file,

helper.js
Code: Select all
function showAlert() {
  alert("I am an alert box!");
}


tests.html :
Code: Select all
<script src="http://internal/helper.js"></script>
<p><img src="http://internal/logo.gif"></p>
<form>
<input type="button" onclick="showAlert();" value="Show Alert">
</form>


The code to load tests.html

Code: Select all
void SimpleApp::OnRunLoopRunning() {
    REQUIRE_UI_THREAD();
   
    // Information used when creating the native window.
    CefWindowInfo window_info;
   
    // SimpleHandler implements browser-level callbacks.
    CefRefPtr<SimpleHandler> handler(new SimpleHandler());
   
    // Specify CEF browser settings here.
    CefBrowserSettings browser_settings;
   
    std::string url;
   
    // Check if a "--url=" value was provided via the command-line. If so, use
    // that instead of the default URL.
    CefRefPtr<CefCommandLine> command_line =
    CefCommandLine::GetGlobalCommandLine();
    url = command_line->GetSwitchValue("url");
    if (url.empty()) {
       url = "http://internal/tests.html";
    }
    // Create the first browser window.
    CefBrowserHost::CreateBrowserSync(window_info, handler.get(), url,
                                      browser_settings, NULL);
 
}


I have no idea why it works for .png and .gif, but it doesn't work for .js and .css, I did add mime_type = "application/javascript"; I checked it in DevTools, the error I got was "Failed to load resource: net::ERR_NAME_RESOLVED http://internal/helper.js".
If you could shed some lights on what I did wrong that would be much appreciated.
mutulong
Newbie
 
Posts: 2
Joined: Tue Apr 15, 2014 7:57 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 70 guests