3

I'm trying to load a local web page executing:

var html = document.open('google.html');
document.documentElement.innerHTML = html;

It loads the page but it's not well formatted and images won't display. How could I load the entire contents?

Thanks

5
  • Call an ajax function and load the page as a requested url. Commented Jun 20, 2012 at 11:00
  • 1
    That's not an ajax problem. But a solution could simply be document.location.href=... Commented Jun 20, 2012 at 11:01
  • I'm sorry but I don't know nothing of ajax, I thought that it was just a combination of javascript, php and xml... I'll try to look up that in google, thanks. Commented Jun 20, 2012 at 11:02
  • @dystroy: yes, that works: the only problem is that it doesn't work for relative paths. In my case, google.html, is in the same directory as the script.js. Commented Jun 20, 2012 at 11:07
  • I'd like to do something like: document.location.href='file://./google.html'; Commented Jun 20, 2012 at 11:09

3 Answers 3

3

Most likely this is because external resources (images, style sheets, js files etc) are allowed to have relative urls. Any relative urls need to be fully qualified for the page to load properly.

Sign up to request clarification or add additional context in comments.

1 Comment

+1: that's the problem but still not the solution because this script is going to be used in an add-on and I can't change relative paths for absolute ones...I want it to be the more general as possible
2

If you want to replace the content of the page by another one, simply do

document.location.href="http://google.com";

If you want to open a local page named google.html do

document.location.href=document.location.href.splitOnLast('/')[0]+"/google.html";

This will ensure that linked resources can be loaded as the relative paths will be accorded to the location of the page. Don't change the content yourself.

5 Comments

I click as answered too soon.. the fact is that it doesn't work because the current url is not the one that points to the script directory
you can unaccept it I think. But I don't get your problem. Are you calling this from a script inlined in the body of your calling page ? This should work. The position of your script should not impact this line.
This script is executed when the add-on (a firefox add-on) detects that the user requests www.google.com/index.html. So, it's not executed on the page itself.
This should not change anything : the document location is the one of the page, not the one of the script. If you really have a problem, try it with window.location instead of document.location. But we're entering a realm very different from the initial question. I didn't recently do FF extensions and I'm not sure about today's solution to inject scripts in the domain of the pages in FF.
But I need the location of my script, the location of the page is www.google.com ...
0

easily just fetch it, and get res as text

var tab = document.getElementById("tab");
function loader(){
    fetch("path-to-file")
          .then(response => response.text())
          .then(text => tab.innerHTML = text);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.