0

I am trying to make a news preview from a separate file (text.html) I want to be able to just display the first 100 characters of a div (id="news"). I don't want to use Ajax or Php. Here is my code, not sure how to make it work, thanks guys.

<body onload="home()">
    <div id="content"></div>
    <script>
        function home() {
            var x = document.getElementById("content").innerHTML = '<object width="100%" height="100%" type="text/html" data="text.html"></object>';
            var pre = x.substring(0,5);
            alert(pre);
        }
    </script>
</body>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Dan Paull
  • 19
  • 4
  • 3
    AJAX or an iframe is the best options here, AJAX being the preferable. Why do you not want to use it? – Rory McCrossan Apr 10 '15 at 10:48
  • Is `test.html` on the same domain? – CodingIntrigue Apr 10 '15 at 10:52
  • I kept getting this error, so i googled it and found that you couldn't load a local file and my file is local. This was the error I was getting XMLHttpRequest cannot load file:///C:/Users/Hillary/Desktop/Mums%20website/text.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. – Dan Paull Apr 10 '15 at 10:53
  • RGraham, both files (text.html) and (test.html) are in the same folder test.html is where i want to load text.html – Dan Paull Apr 10 '15 at 10:56
  • @DanPaull There are extensions for most browsers that allows you to do this if you just want to try it out, but you have to solve it some other way if you want to publish your site. – jokarl Apr 10 '15 at 10:57
  • @hochas how do i manage to get around this, I have looked for a long time and can't work it out – Dan Paull Apr 10 '15 at 10:59
  • @DanPaull Use a [local web server](http://www.maketecheasier.com/setup-local-web-server-all-platforms/), then you'll be able to use AJAX. As Rory says, this is the way to go – CodingIntrigue Apr 10 '15 at 11:10
  • 1
    You say you don't want to use AJAX, but you are using XMLHttpRequest that is the keystone of AJAX. Now, do you have these files running on a server or locally? Your request is "file:///C:/Users/Hillary/Desktop/Mums%20website/text.html" that is local. You should be referring to the file within the server (using relative/absolute paths) instead of referring to the local path of the file as that is generally not allowed (for security reasons) – Alvaro Montoro Apr 10 '15 at 11:12
  • 1
    @AlvaroMontoro I have all of my files running locally. Should I look into exploring running them on a server? if so how hard is it to understand/ learn? – Dan Paull Apr 10 '15 at 11:28
  • 1
    If your problem is just to show the first 100 characters of the text, you can achieve it by the CSS style text-overflow: ellipsis . You can't calculate exactly 100 chars but simply adjust the width of the div to some acceptable width. – Thangadurai Apr 10 '15 at 11:46
  • 1
    @DanPaull, maybe. I think the issue might be there. When you load the file do you use a relative path (i.e.: ./filename.html) or do you use its local path (c:/mydir/filename.html)? – Alvaro Montoro Apr 10 '15 at 11:55
  • @AlvaroMontoro I am using the relative path, should I be using the local path? – Dan Paull Apr 13 '15 at 04:14
  • I think the real issue is that you are running the files without a server. Check these questions: http://stackoverflow.com/questions/17947971/ajax-in-jquery-does-not-work-from-local-file and http://stackoverflow.com/questions/21848540/not-able-to-load-local-xml-file-through-jquery-ajax-call, and maybe try to run the files from a local server instead and let us know if it works – Alvaro Montoro Apr 13 '15 at 04:26

0 Answers0