1

After being very helpfully pointed to AJAX and jquery as an answer to an earlier question, I found much helpful documentation, examples and questions. Particularly this...

Replace innerhtml with external page

But after banging my head against a brick wall for what seems like forever, I just can't get it to work! Hopefully it's something glaringly obvious to you guys! Again, thanks in advance!

Here's what I have...

<head>

	<link rel="stylesheet" type="text/css" href="css/new1.css" />
		
		<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
		
		<script type="text/javascript">
			$(document).ready(
				function navclick(which) {
					$('#container').load(which + '.html');
				}
			)
		</script>
		
</head>

<body id="main">

	<div>
	
		<a href="javascript: void(0)" onclick="navclick('page1')">Page 1</a>
		<a href="javascript: void(0)" onclick="navclick('page2')">Page 2</a>
	
	</div>
	
	<div id="container">
		<p>Placeholder text</p>
	</div>
	
	
</body>

9
  • 1
    Take your method out of the document ready and make it just a normal method and see if your onclicks start working. Commented Nov 21, 2017 at 16:03
  • Thanks for the speedy reply, unfortunately, that doesn't solve the problem. :( Commented Nov 21, 2017 at 16:09
  • Look at your network console and see if it is making the requests, and verify if they are being made that they are returning with a non-error response code Commented Nov 21, 2017 at 16:10
  • I am getting an error by the looks of it... Failed to load file:///E:/Dancefactors%20-%20new%20site/Test/function: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. send @ jquery.js:4 ajax @ jquery.js:4 r.fn.load @ jquery.js:4 navclick @ test.html:10 j @ jquery.js:2 k @ jquery.js:2 setTimeout (async) (anonymous) @ jquery.js:2 i @ jquery.js:2 fireWith @ jquery.js:2 fire @ jquery.js:2 i @ jquery.js:2 fireWith @ jquery.js:2 ready @ jquery.js:2 S @ jquery.js:3 Commented Nov 21, 2017 at 16:18
  • 1
    The first error is telling you that you can't use ajax over file:///. Use a local webserver instead so you can serve over http://. And also do what Taplar said in the first comment - that will remove your Uncaught Reference error. There's no need to define a function within document.ready, since it won't be executed until it's called anyway. document.ready is only to stop code which would normally be executed immediately it was loaded, from executing until the DOM is fully complete. Commented Nov 21, 2017 at 16:30

1 Answer 1

0

I believe this should do it:

$(function() {
  $('.nav').click(function(e) {
    e.preventDefault();
    var url = $(this).attr("name") + '.html';
    $("#container").load(url);
  });
});
<link rel="stylesheet" type="text/css" href="css/new1.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<div>
  <a href="#" name="page1" class="nav">Page 1</a>
  <a href="#" name="page2" class="nav">Page 2</a>
</div>

<div id="container">
  <p>Placeholder text</p>
</div>

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

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.