Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6988049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:01:12+00:00 2026-05-27T19:01:12+00:00

I have a PhantomJS script that loads a local HTML file, injects some javascript

  • 0

I have a PhantomJS script that loads a local HTML file, injects some javascript files, then executes some javascript in the context of the page. The javascript that runs generates an exception, but I only get output from the console, which doesn’t seem to distinguish between an error and a normal log and doesn’t have file, line numbers or a stacktrace.

What I need is a way to capture or otherwise distinguish these errors. I have already tried:

  • Wrapping my PhantomJS script in a try-catch
    • Result: nothing is thrown far enough to be caught by this
  • Define a window.onerror function
    • Result: nothing happens. WebKit does not implement an onerror event on the window

I would prefer to be able to retrieve the error object itself so that I can retrieve the stacktrace.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:01:12+00:00Added an answer on May 27, 2026 at 7:01 pm

    I think there were issues with window.onerror not properly working in WebKit (https://bugs.webkit.org/show_bug.cgi?id=8519). Don’t know if this has been fixed at all, and if so, if the QT WebKit version is already up-to-date.

    However, you should be able to catch the exceptions thrown in your code. If you are using something like webPage.evaluate(...)to run your code, you cannot wrap the complete call in a try/catch block, since the script is evaluated in a different context and the errors will not appear in the main execution context. Insteadyou will need to catch the errors in page execution context. Unfortunately, there is no way of accessing any functions defined in the main context, we therefore have to explicitly write the wrapping code around your code to be executed.

    The following is a modified example of the phantomwebintro.js file as included in the PhantomJS source. It loads an HTML page, inserts a script and then runs some code in the page context (here with a line throwing a type error). This code is wrapped with a try/catch block and will return the wrapped result or error object to the main context.

    ...
    
    // Load an HTML page:
    page.open("http://www.phantomjs.org", function(status) {
        if (status == "success") {
    
            // Inject some scripts:
            page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
    
                // Run your own code in the loaded page context:
                var resultWrapper = page.evaluate(function() {
                    var wrapper = {};
                    try {
                        // Your code goes here
                        // ...
    
                        var x = undefined.x; // force an error
    
                        // store your return values in the wrapper
                        wrapper.result = 42;
                    } catch(error) {
                        wrapper.error = error;
                    }
                    return wrapper;
                });
    
                // Handle the result and possible errors:
                if (resultWrapper.error) {
                    var error = resultWrapper.error;
                    console.log("An error occurred: " + error.message);
                    // continue handling the error
                    // ...
                } else {
                    var result = resultWrapper.result;
                    // continue using the returned result
                    // ...
                }
    
                ...
    
            });
        }
    });
    
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small project containing 1 HTML file and 4 JavaScript files. Recently,
Have some dates in my local Oracle 11g database that are in this format:
I have a few pure-JavaScript, client-side tests using PhantomJS. These I'd like to integrate
I've got a script that runs in PhantomJS on my server that I need
I'm using a rake script to tag the AssemblyInfo.cs files in my .NET projects,
I have a table that contains sets of sequential datasets, like that: ID set_ID
Have a photography site that I want to prevent image copying from. How can
I run Ruby threads that use PhantomJS, a command-line headless Webkit engine, to turn
I have an SQLite query in my android app that seems to crash when
Someone had changed my .htaccess, and I have removed that. But I still have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.