简体   繁体   中英

Can I get the address bar url from the javascript console when the page has failed to load?

Just say I typed in a bad hostname in the address bar.

For example, say I wasn't running a local web server, and I load:

http://localhost/callback_url

In Chrome, this will give me a "This webpage is not available" message.

Is there anyway I can find out what the url is in the address bar from the Javascript console, even though the page failed to load?

I know I can normally use window.location.href to get this, but that returns "data:text/html,chromewebdata" in this instance.

So in this example, I'd like to know if there's some javascript that returns http://localohost/callback_url

EDIT: The main reason I'd like to do this is so I know if server side redirect failed when using ChromeDriver with Selenium. So I'd prefer to avoid using extensions if possible, and am open to Chrome and ChromeDriver specific solutions if applicable! The callback_url may have extra info in it, added by the server, and I'd like to see what this info is. I'd like to avoid running another server to get this data if possible.

在此输入图像描述

The loadTimeData object included in the ERR_CONNECTION_REFUSED page has the failed URL:

> loadTimeData.data_.summary.failedUrl
"http://localhost/foo?request_url=bar"

You can get it from the title of the page. By typing document.title and doing some regex you can get the URL.

Another way I found is by using the following

var data = loadTimeData.createJsEvalContext();
console.log(data.a.$top.summary.failedUrl);

If you open the developer tools and search for a part of the URL in source code, you will see that Chrome creates the loadTimeData in the "not available page".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM