0

I know this has been asked multiple times but I can't figure out why this won't work for me. Trust me, I've done research but I still can't see what's wrong with this. I'm also a complete noob at html and javascript/jquery so please be nice :)

My goal is to load a simple text from text.html onto index.htmlusing jQuery.

This is my code as of right now

index.html

<head>
<script src="jquery-3.4.1.min.js"></script>
<script>
$(document).ready(function(){
   $('#content').load("text.html");
});
</script>
</head>

<body>
<div id="content"></div>
</body>

text.html

<body>
<p>Hello there!</p>
</body>

I have jQuery 3.4.1 in the same folder as these two files as well. When I drag index.html into Google Chrome I get a blank page. Of course, if I had any text in index.html it did show up when I loaded the html.

I'd also like to mention that I'm using <div> because I'm trying to do the same thing on another project by loading a div onto an html, so I'm seeing if even this small code would work, but sadly it's not. I've changed <div> in index.html into <p> and it made no difference. Any help would be greatly appreciated!

Edit: My goal is to have "hello there!" display. When I drag index.html into my web browser to preview what my output should look like, "Hello there!" does not display - instead, I get a blank page. I've tested index.html to have some other text in it, and it displays perfectly fine. The problem is that code from text.html doesn't seem to display.

xerop
  • 67
  • 1
  • 1
  • 7
  • 3
    What do you mean by "won't work?" What errors are you getting? – matthew-e-brown Jan 19 '20 at 03:39
  • 5
    The basic code shown should work if paths to jQuery.js and text.html are correct relative to your index.html page. Also assumes you are not running this in `file://` protocol. `load()` will not work in `file://` – charlietfl Jan 19 '20 at 03:41
  • i test your code , its work to me , please explain what happend to your code when run it ? – Neo Anderson Jan 19 '20 at 03:41
  • 1
    When you just drag the file into chrome you are opening it in `file://` protocol. Will need to run a localhost server to use ajax. Lots of ways to easily set up a localhost server – charlietfl Jan 19 '20 at 03:47
  • 1
    Can you move your script below the body tag. To be sure that your script will run after the html has loaded. – Qonvex620 Jan 19 '20 at 04:19
  • 1
    Have a look at https://stackoverflow.com/a/20578692/7874305 – Bambam Deo Jan 19 '20 at 05:48

0 Answers0